WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#24386 new enhancement

Make _pad_term_counts work for non-post type objects and attachments

Reported by: TomAuger Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

If you register a hierarchical taxonomy against a non-post object (such as users), or attachments that aren't associated with another post, _pad_term_counts does bubkus, due to the INNER JOIN on $wpdb->posts, and the check for 'publish' status (which is relevant for attachment post types).

I'm suggesting an alternative approach would be to use $term->count, and eschew going back to the database altogether.

Change History (5)

#1 @ocean90
5 years ago

Related: #22558

#3 @wonderboymusic
5 years ago

  • Keywords needs-patch added

#4 @boonebgorges
4 years ago

  • Keywords needs-unit-tests added

I'm suggesting an alternative approach would be to use $term->count, and eschew going back to the database altogether.

I agree that it's bad to have this function so closely tied to posts. But simply incrementing 'count' seems risky, as there'd be no checks to make sure these numbers stayed in sync.

Some unit tests for this functionality would be hugely helpful, as it'd give us a better sense of when these counts ought to be changing.

#5 @boonebgorges
3 years ago

  • Milestone changed from Awaiting Review to Future Release

Circling back around to this, I think I agree that we should be using $term->count, and not doing a separate database query. I'm not sure why the query was ever necessary; see [6159], [6087], [4707] for some relevant history. The 'post_type' information does not seem important, since only the proper object_types should be reflected in the 'count' field; likewise for the 'post_status' check.

The key will be to write some unit tests that verify that the 'count' field is correctly recalculated on 'transition_post_status'. This should already be happening at _update_term_count_on_transition_post_status(), and the term count callback functions (_update_post_term_count() and _update_generic_term_count()) should already be accounting for post_type/post_status stuff, but this needs verification via tests.

Note: See TracTickets for help on using tickets.