WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14788 closed defect (bug) (duplicate)

get_categories returns (sub)categories with drafts

Reported by: illutic Owned by:
Milestone: Priority: normal
Severity: major Version: 3.0.1
Component: General Keywords: get_categories 2nd-opinion
Focuses: Cc:

Description

As stated in this topic, categories with drafts only are also included in the get_categories list.

The code I'm using (to get subcategories, it's inside functions.php):

/* subcategories */
	$args = array(
					'type'			=> 'post',
					'child_of'		=> $id
				);
    foreach (get_categories($args) as $cat)
	{
    	echo '<p><span class="subcategory">';
						
        $apps = $cat->category_count == 1 ? 'apartment' : 'apartments';
		
        echo '<a href="'.get_category_link( $cat->cat_ID ).'">'.$cat->cat_name.'</a> <small> ('.$cat->category_count.' ' . $apps . ')</small>';
        if ($cat->category_description != '')
		{
        	echo '<br />'.$cat->category_description . ' <a href="'.get_category_link( $cat->cat_ID ).'">View the apartments &raquo;</a>';
        }
        
		echo '</span></p>';
	} // end foreach

Change History (7)

comment:1 @illutic4 years ago

the code from this post resolves this issue.

comment:2 @mdawaffe4 years ago

  • Keywords 2nd-opinion added

I can't reproduce this. Related to #14344 ?

comment:3 @scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This is merely a consequence of #14076

comment:4 @scribu4 years ago

Or rather #14084, to be more exact.

comment:5 @illutic4 years ago

I'm not sure how my ticket would be related to 14344, #14076 or #14084. All three tickets are about the count that is being displayed.
I am talking about actually displaying the posts where drafts are included.

comment:6 @scribu4 years ago

The code you provided as an example doesn't display any posts.

comment:7 @scribu4 years ago

Also, if the post count didn't include draft posts, those subcategories wouldn't show up unless you set 'hide_empty' => false. That's why it's a duplicate.

Note: See TracTickets for help on using tickets.