#41813 closed defect (bug) (wontfix)
get_terms is hiding taxonomies that are not empty
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.8.1 |
Component: | Taxonomy | Keywords: | has-patch |
Focuses: | Cc: |
Description
I have created a custom taxonomy for media type contents which has three terms in it. All terms have at least one image (media) associated to it. When I use the following code:
<?php $terms = get_terms( array( 'taxonomy' => 'media_content_category', 'hide_empty' => true )); foreach($terms as $term) { echo $term->name . '<br />'; }
it only displays one taxonomy when there should be all of them as no taxonomy is "empty".
NB: All plugins are deactivated during test.
Attachments (3)
Change History (13)
#2
in reply to:
↑ 1
@
7 years ago
- Keywords reporter-feedback removed
@boonebgorges I have created this custom taxonomy and associated it with attachment (not a typical custom post type). Here is my code to register the custom taxonomy:
<?php add_action( 'init' , 'emfl_create_media_content_taxonomy' ); function emfl_create_media_content_taxonomy() { register_taxonomy_for_object_type( 'media_content_category', 'attachment' ); } add_action('init', 'emfl_media_content_taxonomy', 0); /* Creating Custom Taxonomy */ function emfl_media_content_taxonomy() { $labels = array ( 'name' => _x('Media Content Categories', 'Media Content Categories'), 'singular_name' => _x('Media Content Category', 'Media Content Category'), 'search_items' => __('Search Media Content Categories'), 'all_items' => __('All Media Content Categories'), 'parent_item' => __('Parent Media Content Category'), 'parent_item_colon' => __('Parent Media Content Category:'), 'edit_item' => __('Edit Media Content Category'), 'update_item' => __('Update Media Content Category'), 'add_new_item' => __('Add New Media Content Category'), 'new_item_name' => __('New Media Content Category Name'), 'menu_name' => __('Content Category') ); /* Registering taxonomy for "attachment" */ register_taxonomy('media_content_category', array('attachment'), array ( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug', 'content_category') )); }
Replying to boonebgorges:
hide_empty
checks against theterm_taxonomy_id.count
column. My guess is that the values in this column are '0' for the missing terms. It's possible that this is because you're missing some necessary logic for updating term counts. Can you share the code you're using to register your media_content_category taxonomy? Is it associated with a custom post type, or some other type of object? Theupdate_term_callback
param forregister_taxonomy()
might be where you start your debugging.
#3
@
20 months ago
Confirmed
the query is simple and returns all 3
SELECT t.term_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('media_content_category') ORDER BY t.name ASC
however .. the count IS actually zero for all but the first one, see image which I will add now
#4
@
20 months ago
The issue is in _update_post_term_count
on
// Attachments can be 'inherit' status, we need to base count off the parent's status if so. if ( $check_attachments ) {
this code does only account for attachments that are inserted fresly (uploaded) on a CPT and not single attachments
if I run this manually for item 5 which is used 2 times the output is actually 2
SELECT COUNT(*) FROM wp_term_relationships, wp_posts p1 WHERE p1.ID = wp_term_relationships.object_id AND ( post_status IN ('publish') OR ( post_status = 'inherit' -- AND post_parent > 0 -- AND ( SELECT post_status FROM wp_posts WHERE ID = p1.post_parent ) IN ('publish') ) ) AND post_type = 'attachment' AND term_taxonomy_id = 5;
It goes wrong on the post_parent which is zero AND the second line for there is no post_parent to check
#8
@
16 months ago
- Keywords needs-unit-tests removed
- Resolution set to wontfix
- Status changed from new to closed
As much as I wanted to create a patch for this, I can confirm this will work when using this :
<?php register_taxonomy('media_content_category', array('attachment'), array ( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug', 'content_category'), 'update_count_callback' => '_update_generic_term_count', ));
Please note the update_count_callback as this will simply do a no-nonsence call for all objects with the term.
hide_empty
checks against theterm_taxonomy_id.count
column. My guess is that the values in this column are '0' for the missing terms. It's possible that this is because you're missing some necessary logic for updating term counts. Can you share the code you're using to register your media_content_category taxonomy? Is it associated with a custom post type, or some other type of object? Theupdate_term_callback
param forregister_taxonomy()
might be where you start your debugging.