I have a custom taxonomy called manufacturer. It's created via an init hook. I've verified that the taxonomy exists in wordpress by print_r(get_taxonomies()); I've also verified that a post has the taxonomy by doing print_r(get_the_terms(66878, 'manufacturer')); which shows me this:
Array ( [0] => WP_Term Object ( [term_id] => 6957 [name] => Agilent [slug] => 6-agilent [term_group] => 0 [term_taxonomy_id] => 6957 [taxonomy] => manufacturer [description] => [parent] => 0 [count] => 889 [filter] => raw ) )
The site was working fine up until a few days ago. I verified the DB. Innodb and nothing appears corrupted. The data in the DB looks correct. If I run this code I get no results:
$args = array(
'taxonomy' => 'manufacturer',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0
);
$all_categories = get_categories( $args );
or this:
print_r(get_terms( 'manufacturer', [
'hide_empty' => false,
]));
Git shows no file changes. Also, dev and live both have this issue even though dev has not been updated data-wise in weeks. The only event that happened recently was the server got restored from an image. After the restoration, the manufacturer taxonomy was working properly. Also, this is not the only custom taxonomy that isn't functional anymore. There is at least one other one.
I went to edit the wp-config.php file on the server and I couldn't save the file. The disk was full.
While the docs says get_categories can be used here, I always prefer to use get_terms when retrieving taxonomy terms. I don't think this will make the difference but that would be my first attempt.
Furthermore, you defined some default values, which are not needed imo:
hierarchical is true by default
orderby is name by default
pad_counts is false by default
I wouldn't define what is default.
Related
I recently added taxonomies to my user settings page using this tutorial and the ACF taxonomy field, although I didn't see anyway to query on taxonomy based on this documentation I figured I would just try it using these arguments:
$args = [
'role' => 'gebruiker',
'number' => $limit,
'offset' => $offset,
'tax_query' => array(
array(
'taxonomy' => 'category_level',
'terms' => 36,
'field' => 'term_id',
)
)
]
But it doesn't work and just returns every user that matches the other arguments, so I was wondering if it is at all possible to query based on taxonomy and if so, how do I do this? :)
Altough I never did figure out the issue, I did find a solution that works for me ;)
$args = [
'key' => 'specialty',
'value' => '47',
'compare' => '='
]
WP_User_Query($args);
Returns only 2 users, which is what I expected since only 2 users have the specialty taxonomy set to a term with the ID 47! But as soon as I wrapped this query in a meta_query tag (see below) so I could add more parameters such as the users role. It would return every user matching the role query.
$args = [
'meta_query' => [
'key' => 'specialty',
'value' => '47',
'compare' => '='
]
]
WP_User_Query($args);
So, what I ended up doing was writing a custom MySQL query to fetch the ids of the users which had to custom taxonomy set, afterwards I could use the normal WP_Query with the role parameter and I just added the include parameter with the previously found IDs
I'm using get_pages in a Wordpress theme to create the nav.
I have some pages I don't want to show in the nav.
On all pages I have a 'dont_show_in_nav' true/false custom field.
I can use meta_key to add pages that have 'dont_show_in_nav' selected
but I'd like to not show the pages that have 'dont_show_in_nav' selected.
I could create a 'show_in_nav' custom field and select all the pages to show but I have to many pages to do that.
I've tried 'meta_value' => false
$pages_args = array(
'sort_column' => 'menu_order',
'parent' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'meta_key' => 'dont_show_in_nav',
'meta_value' => true
);
I'm guessing that the issue is that meta_value has to be a string since WordPress only supports strings for post meta. When you create your field, what are you actually saving? Is it the word "true", is it a '0', a '1' etc?
Regardless, a much better method for generating nav menus in wordpress is to take advantage of the nav menu functions. Take a look at wp_nav_menu(), that should work much more reliably than a meta key.
As of now Wordpress documentation for get_pages() says the function doesn't support querying with a meta_key:
NOTE: This function will currently not retrieve pages using the 'meta_key' and 'meta_value' parameters in the $args array as described below. Until this is fixed in the WordPress codebase, you may want to use get_posts() instead.
So your code should call get_posts() instead:
$pages_args = array(
'orderby' => 'menu_order',
'parent' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'meta_key' => 'dont_show_in_nav',
'meta_value' => 'true'
);
$pages = get_posts($pages_args);
Note that I quoted 'true', since to Wordpress it's just a text field. Also, some arguments for get_posts are slightly different than for get_pages.
I have a custom WP Query where I list custom post types. I am trying to set up filters to filter listings by custom taxonomy.
My query setup is as follows:
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => $postsperpage,
'posts_per_archive_page' => $postsperpage,
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
So in the above example I should get only posts with taxonomy 'sector' with slug 'business-services', but ALL posts from custom post types 'opps' get listed instead.
I have looked everywhere on stackoverflow and beyond but I just can't figure out what I've been doing wrong.
Your code looks fine.
I have had this issue before because wordpress does not sync taxonomy terms until late in the init phase. Have you tried running this code in a template, after get_header() or wp_head() has been called? If not try that, it should work then. I know if I've echoed it out in functions.php for testing it will not work because wordpress has not synced tax terms yet.
You should really have a look at the codex (WP_Query) when creating custom queries. As your code stands, it will fail. You have the following issues
pagination is not a valid parameter for WP_Query.
You cannot use posts_per_page and posts_per_archive_page together
You don't need to set parameter values that are set by default. Unnecessary parameters used in your code which is by default the default values are order, orderby, post_status, cache_results, update_post_meta_cache and update_post_term_cache
There is no sticky post functionality for custom post types. just remove the ignore_sticky_post parameter
Your code should look something like this ( I hope your variables is defined )
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
Context: Using Wordpress with jigoshop plugin
I am using wp_list_categories to bring product categories that working well and brings the required list. The only problem is the ordering of the categories. I have used several orderby options (name, ID, slug) but the list order remains still the same as follows:
Tools & Brushes
Makeup Remover & Primer
Powder
Lips
Highlighter
Foundation
Eyes
Concealer
Bronzer
Blush
The jigoshop widget has following code:
$args = array(
'orderby' => 'name',
'show_count' => $count,
'hierarchical' => $is_hierarchial,
'taxonomy' => 'product_cat',
'title_li' => null,
);
wp_list_categories(apply_filters('widget_product_categories_args', $args));
I am going to replace it by adding filter:
add_filter('widget_product_categories_args','myFun');
function myFun($out){
.......
.......
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 0,
'child_of' => $topMostParent,
'hierarchical' => 1,
'title_li' => '',
'current_category' => $cur_cat,
'taxonomy' => $taxonomyName,
);
return $args;
}
I have tried by deactivating other plugins too but got no effect on result. Please help.
Thanks.
Most likely,
Somewhere another filter is changing the arguments these can be.
In order they are executed:
get_categories_taxonomy
get_terms_args
get_terms
get_terms_orderby <-- most likely?
list_terms_exclusions
get_terms_fields
terms_clauses
get_terms (again)
get_terms (again again)
wp_list_categories
These are divided over 3 primary functions ( functions which are likely to affect the outcome)
wp_list_categories
get_categories
get_terms
I suggest starting with the get_terms_orderby filter.
If that doesn't work I would try to use the function get_terms
That way you will at least know if at which level it goes wrong.
Hopes this helps, let me know ;)
I have a custom post type clientgallery and the custom taxonomy client.
To get all galleries I can type website.com/clientgallery.
But I want to show only galleries of a specific client, like: website.com/clientgallery/miller
So, miller should act like a get parameter.
I already know how to get the galleries by client, but I don't know how to get the parameter part working.
$args = array(
'numberposts' => -1, //limit the number of posts, set 0 if no limit required.
'orderby' => 'post_date', //order by post_date field.
'order' => 'DESC', //order by descending oder.
'post_type' => 'clientgallery', //the post type is custom post type 'News & Events'
'post_status' => 'publish', //post status is 'publish'
'tax_query' => array(
array(
'taxonomy' => 'client', //custom taxonomy slug
'field' => 'slug', //select taxonomy term by slug
'terms' => $_GET['client'] //taxonomy term is called 'home-page'
)
));
Call it taxonomy-client.php :
See all info here on template hierarchy for WordPress : http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
If your taxonomy client is only associated with the clientgallery post type, then website.com/client/miller should suffice to show your list of clientgalleries for this client. I tested it on my site, it works. Or am I getting something wrong?