Support » Developing with WordPress » hide_empty is ignored if category list is called multiple times

  • Resolved Kenji Yanagiya

    (@childsview)


    Hello,
    Sorry if this is a known issue.

    If the category list is called multiple times on the same page, hide_empty is ignored.
    This problem occurs when there is a category to which a post not included.

    Environment.
    WordPress: 6.0;
    Language: Japanese;

    This phenomenon occurs starting with 6.0.
    It did not occur in 5.0.

    For example, the following code

    function my_category(){
    	$args = array(
    		'title_li'		=> '',
    	);
    	return wp_list_categories($args);
    }
    echo my_category();
    echo my_category();

    This problem occurs with all of get_terms(), get_the_category(), and wp_list_categories().
    It occurs when called multiple times within the same page, even if the template is divided into header.php, footer.php, etc.

    My expected behavior is for hide_empty to work no matter how many times it is called within the same page.
    My workaround is the following code.

    function my_function()
    {
    	$terms_cache = get_transient('my_list');
    	if($terms_cache){
    		return $terms_cache;
    	}
    	$args = array(
    		'title_li'		=> '',
    		'echo'			=> 0,
    		'taxonomy'		=> 'category',
    	);
    	$terms = wp_list_categories($args);
    	if(is_wp_error($terms) || !($terms)) return '';
    	set_transient( 'my_list', $terms, 1 * HOUR_IN_SECONDS );
    	return $terms;
    }

    It is executed using transient.
    This method works as expected.

    Please confirm this. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You’re not using hide_empty anywhere?

    Thread Starter Kenji Yanagiya

    (@childsview)

    Thank you for reply, Jacob.
    I was trying different things, so the code was different from the first phenomenon code. Sorry.
    hide_empty was omitted because it is true by default.
    The code is described again.

    function my_category(){
    	$args = array(
    		'title_li'		=> '',
    		'hide_empty'		=> true,
    
    	);
    	return wp_list_categories($args);
    }
    echo my_category();
    echo my_category();

    The value of hide_empty was the same whether it was true or 1.

    Please confirm this. Thank you.

    Thread Starter Kenji Yanagiya

    (@childsview)

    I have learned that this issue will be addressed in the following ticket and I will close it. Thank you.

    https://core.trac.wordpress.org/ticket/55837

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.