Get multiple categories names from permalink Wordpress - wordpress

I am trying to get categories names for the ones that are available in permalink. e.g. I have the following permalink structure.
abc.com/product-category/category-1/category-2/
Where as category-1 and category-2 both are categories. When I try $wp_query->get_queried_object(), it just returns me info related to category-2. How can I get details of category-1 using any existing wordpress functions?
Thanks

As the previous categories are parent of the current use can get it using get_term https://developer.wordpress.org/reference/functions/get_term/
$child_category = $wp_query->get_queried_object();
$parent_category = get_term($child_category->parent, 'product-category');

You could get the parent of the category-2 using the get_term function.
$child_term = get_queried_object();
$parent_term = get_term($child_term->parent, 'product_cat'); // if your using Woocommerce then the taxonomy is called 'product_cat'
Both of these variables will be WP_Term objects.
https://developer.wordpress.org/reference/classes/wp_term/
From there you can access the name, id and any other information you need.
Revised
If you're wanting to create a custom category page (i.e. not a Woocommerce/WordPress page) then I would recommend adding a meta-box to the page editor.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://www.advancedcustomfields.com/
Have a select box with your product categories.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Then in your page template, use the term(s) ID as part of your WP_Query to pull products from both of those product categories.

Related

Adding custom display_type to WooCommerce categories (product_cat taxonomy)

Under a category, WooCommerce ships with a field called 'display_type'. It has 4 values:
Default
Products
Subcategories
Both
How do I add more options in there?
I found the function in the source code: add_category_fields and it doesn't look like that there is a hook, where additional fields can be added.
Had I been lazy, then I would probably just have added a new meta-field with my customly added options (using ACF).
Bonus question: For some reason, then I can't get the value of the display_type-field, unless using $wpdb. $display_type = get_term_meta( $term->ID, 'display_type' ); Doesn't seem to work. I elaborated on this question over here.

Getting taxanomy value from URL

I'm setting up a search page by views exposed filter. And one of the field are filter by taxonomy term.
For example, when I search with the taxonomy term filter field, the URL is like below.
domain.com/search?subjects=69
Now I wish to get the value of the taxonomy (it's showing tid instead of value)
<?php
$idenity = $_GET['subjects'];
print $idenity;
?>
Anyway to get the value of the taxonomy value but not taxonomy id?
You didn't specify which version you're using so I'm assuming Drupal 7 because it's the latest stable version.
You can load the term with taxonomy_term_load():
<?php
$tid = intval($_GET['subjects']);
$term = taxonomy_term_load($tid)
print $term->name;
?>
Personally, I find that the comments on the Drupal API site are usually very helpful in understanding how to use functions that sound like they are relevant to my problem.

Is it possible to convert wordpress custom post type to normal categories

Currently our site's videos are posted as custom post type. We have totally changed the theme and added so many new features. For our new site we are trying to change videos to category. (Custom post type to normal categories). Is this possible. I have exported the custom post type as XML file, but I don't know how to change it to categories.
Finally an answer. Before I start These are the conditions I'm using.
The Custom posttype (called video) need to be merged into the default post
Video's has it own custom categories, who needs to be merged into the default categories
All Video's need to get a category video, the old categories need to be children of the newly created video category.
Step Zero: BACKUP my sql BACKUP
First create a the video category in the default posttype. Note it's ID (it is found in the category edit page in the url under tag_ID. I'm assuming ID 4 in my examples
Second we assign this category to the video posttype with the following Sql:
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`)
SELECT `ID`, '4' FROM `wp_posts` WHERE `post_type` = 'video';
Next we assign the existing custom categories as children. You need the video category slug, found on the custom-category page agian in the url with taxonomy.
UPDATE `wp_term_taxonomy` SET `parent` = 4, `taxonomy` = 'category' WHERE `taxonomy` = 'VIDEO CATEGORYSLUG';
The last step is assigning the normal posttype to the custom posttype.
UPDATE `wp_posts` SET `post_type` = 'post' WHERE `post_type` = 'video';
Now it should be done and everything should be merged. So TEST TEST TEST.
After that the code making the posttype can be disabled.
This should work, if you have questions just ask.

getting posts from multiple categories in blog category template

I have 5 blog sections each representing a category. However, I would like to have a category called "all" that if checked would include the post in all 5 section without having to click every category.
I guess the only way to do this is to add something into the blog category template file to tell it to include the usual category posts as well as the "all" category.
now I think i've located where it queries the post:
if ( $wp_query->have_posts()) : while (have_posts()) : the_post();
but there is nothing identifying the current category id, except for a few things above for specifying the right title for the theme.
What would be the best way of achieving what I have explained?
You can interact with the wp_query object... the same way that you interact with query_posts()... it accepts the same arguments.
For either you can use Category Parameters as follows (from the wordpress codex: query_posts function reference)
Category Parameters
Show posts associated with certain categories.
cat (int) - use category id.
category_name (string) - use category
slug (NOT name).
category__and (array) - use category
id.
category__in (array) - use category
id.
category__not_in (array) - use
category id.
When it references $wp_query, the variables of the query have already been defined, you can set up your own query with any parameteres you want.

Wordpress Archive by Year "query_posts()" Problem

I created an Archive list (also this is my archive.php codes) on my blog and i want to show posts by years.
But when i use query_posts() function for excluding some categories and limit posts then it's showing all posts not by year.
For example this page is showing all posts not only posts in 2009 years.
So if i summarize this issue; i want to show archive list by year (when i enter /2009/ permalink)+exclude some categories and limit posts.
Thank you.
maybe when you use global variable, $query_string, it will help.
so you must use it like this
global $query_string;
query_posts($query_string . '&cat=-13,-4,-14,-171&posts_per_page=5&paged='. $paged);
as codex mention, here : query post,
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

Resources