WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#41813 new defect (bug)

get_terms is hiding taxonomies that are not empty

Reported by: subrataemfluence Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8.1
Component: Taxonomy Keywords:
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.

Change History (2)

#1 follow-up: @boonebgorges
3 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
3 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 3 years ago by subrataemfluence (previous) (diff)
Note: See TracTickets for help on using tickets.