Make WordPress Core

Opened 6 years ago

Closed 4 months ago

Last modified 4 weeks ago

#41813 closed defect (bug) (wontfix)

get_terms is hiding taxonomies that are not empty

Reported by: subrataemfluence's profile subrataemfluence 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)

Screenshot from 2023-06-08 15-29-46.png (59.8 KB) - added by ramon fincken 9 months ago.
database view, look at the count column
Screenshot from 2023-06-08 15-42-40.png (67.2 KB) - added by ramon fincken 9 months ago.
the relationships, they are there
patch.41813.20230608.1.diff (1.2 KB) - added by ramon fincken 9 months ago.
patch.41813.20230608.1.diff

Download all attachments as: .zip

Change History (13)

#1 follow-up: @boonebgorges
6 years ago

  • Keywords reporter-feedback added

hide_empty checks against the term_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? The update_term_callback param for register_taxonomy() might be where you start your debugging.

#2 in reply to: ↑ 1 @subrataemfluence
6 years ago

  • Keywords reporter-feedback removed

@boonebgorges I have created this custom taxonomy for associating with attachment. Here is the code:

<?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 the term_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? The update_term_callback param for register_taxonomy() might be where you start your debugging.

Last edited 6 years ago by subrataemfluence (previous) (diff)

#3 @ramon fincken
9 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

@ramon fincken
9 months ago

database view, look at the count column

@ramon fincken
9 months ago

the relationships, they are there

#4 @ramon fincken
9 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

#5 @ramon fincken
9 months ago

created patch

@ramon fincken
9 months ago

patch.41813.20230608.1.diff

#6 @ramon fincken
9 months ago

  • Keywords has-patch added

#7 @ramon fincken
7 months ago

  • Keywords needs-unit-tests added

#8 @ramon fincken
4 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.

#9 @ramon fincken
4 months ago

I have submitted documentation with links to this ticket at the developer pages of register_taxonomy and get_terms.

#10 @swissspidy
4 weeks ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.