Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 2 years ago

#15806 closed defect (bug) (worksforme)

get_terms with hide_empty returns count > 0 with unpublished items

Reported by: bendog Owned by:
Milestone: Priority: normal
Severity: major Version: 3.0
Component: Taxonomy Keywords:
Focuses: Cc:



I think that's a bug.

I've defined a custom post type and a custom category taxonomy for it.

On my front end, i'm using get_terms with hide_empty to show the categories of the existing custom post type.

get_terms does ignore unpublished posts but it seems to include trashed item.

I have one category that is linked to only a trashed item and the function includes it in the count.

I can't select 3.0.3 in the version dropdown but it happened on any 3.0.x that I tried.

Change History (10)

comment:1 @scribu5 years ago

  • Cc scribu added

comment:2 @neoxx4 years ago

  • Cc neo@… added
  • Keywords changed from taxonomy, custom post to taxonomy custom post

In 3.2 I could also reproduce the bug with an unpublished post in combination with an otherwise not assigned custom taxonomy term. (get_terms w/ hide_empty)

comment:3 @vtowel4 years ago

  • Summary changed from get_terms with hide_empty returns count > 0 with trashed items to get_terms with hide_empty returns count > 0 with unpublished items

I could also reproduce this bug with posts pending approval. Their tags/categories shouldn't show up in the front end if those tags/categories are otherwise unused by published posts. Even better would be for get_terms to have an option specifying desired post status, "publish" being the default value.

comment:4 @SergeyBiryukov4 years ago

  • Keywords needs-patch added; taxonomy custom post removed

comment:5 @jtwg4 years ago

Just ran into this same problem. Custom post type with custom taxonomy, custom taxonomy returning results with posts in trash.

comment:6 @jgadbois4 years ago

  • Cc jgadbois added

Seems like either a join will be needed in get_terms() or wp_update_term_count_now() will need to be modified to only count terms tied to published posts. This second method will also require counts to be updated when post status changes.

I'd be happy to look at this one...anyone want to chime in as to which approach would be preferred?

comment:7 @paudelvinay4 years ago

  • Cc paudelvinay added
  • Severity changed from minor to major
  • Version changed from 3.0.2 to 3.2.1

Is there any progress on get_terms with unpublished post/private posts for custom taxonomy?

comment:8 @SergeyBiryukov4 years ago

  • Version changed from 3.2.1 to 3.0

Version number indicates when the bug was initially introduced/reported.

comment:9 @barrykooij3 years ago

  • Cc wpcore@… added
  • Resolution set to worksforme
  • Status changed from new to closed

I tried to reproduce this bug in version 3.6-alpha-23791 and it appears to working correctly now.

Version 0, edited 3 years ago by barrykooij (next)

comment:10 @markoheijnen2 years ago

  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.