WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 6 months ago

Last modified 5 months ago

#38843 closed enhancement (fixed)

Add filter for post statuses in _update_post_term_count()

Reported by: GunGeekATX Owned by: adamsilverstein
Milestone: 5.7 Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch has-unit-tests commit has-dev-note
Focuses: Cc:

Description

_update_post_term_count() is hard-coded to only use the 'publish' status when updating the post count for a term. The lack of a filter here does not allow the term count to include other post statuses (or custom post statuses) in situations where the user would like the term count to reflect the other statuses.

Example of using proposed patch to allow 'future' status to be included in Category term count.

add_filter( 'update_post_term_count_statuses', function( $post_statuses, $taxonomy ) {

	// Allow future statuses to be counted for Category post count.
	if ( 'category' === $taxonomy->name ) {
		$post_statuses[] = 'future';
	}

	return $post_statuses;
}, 10, 2 );

Attachments (7)

38843.patch (1.9 KB) - added by GunGeekATX 5 years ago.
Proposed patch with filter
38843.diff (5.1 KB) - added by adamsilverstein 7 months ago.
38843-1.diff (5.2 KB) - added by nwjames 7 months ago.
Modified the SQL to change the second occurence of "= 'publish
38843.2.diff (5.0 KB) - added by audrasjb 6 months ago.
patch refreshed against trunk
38843-before-after.gif (5.7 MB) - added by hellofromTonya 6 months ago.
Works: tested before and after applying patch 38843.2.diff
38843.4.diff (5.1 KB) - added by hellofromTonya 6 months ago.
Refreshed to fix failing test and phpcs errors.
38843.5.diff (5.1 KB) - added by hellofromTonya 6 months ago.
Changes @param from int to WP_Taxonomy. Props Timothy.

Change History (28)

@GunGeekATX
5 years ago

Proposed patch with filter

#1 @GunGeekATX
5 years ago

  • Keywords has-patch added

#2 @adamsilverstein
3 years ago

  • Keywords needs-unit-tests added
  • Owner set to adamsilverstein
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core by davecpage. View the logs.


3 years ago

#5 @adamsilverstein
7 months ago

In 38843.diff: Updated patch and added test coverage for new filter.

#6 @adamsilverstein
7 months ago

  • Milestone changed from Awaiting Review to 5.7

#7 @adamsilverstein
7 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

This ticket was mentioned in PR #838 on WordPress/wordpress-develop by adamsilverstein.


7 months ago

Trac ticket:

@nwjames
7 months ago

Modified the SQL to change the second occurence of "= 'publish

#9 @nwjames
7 months ago

Sorry my comment with the modified diff was a little cryptic.

The SQL statement is rather long and contained 2 occurrences of "= 'publish'" (one for posts and the other for attachments).

The previous fix addressed only the first one.

This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.


6 months ago

#11 @hellofromTonya
6 months ago

  • Keywords needs-dev-note added

Adds a new filter. Noting needs inclusion into the misc dev note.

@audrasjb
6 months ago

patch refreshed against trunk

#12 @audrasjb
6 months ago

@hellofromTonya @SergeyBiryukov patch and unit tests refreshed as per today's bug scrub.

@hellofromTonya
6 months ago

Works: tested before and after applying patch 38843.2.diff

#13 @hellofromTonya
6 months ago

  • Keywords commit added

Attached gif shows the changes work. Here's the hooked callback I used in a must-use file:

<?php
add_filter(
        'update_post_term_count_statuses',
        function ( $statuses, $taxonomy ) {
                if ( 'category' === $taxonomy->name ) {
                        $statuses[] = 'draft';
                        $statuses[] = 'future';
                        $statuses[] = 'pending';
                }

                return $statuses;
        },
        10,
        2
);

Marking this ticket ready as a commit candidate.

Last edited 6 months ago by hellofromTonya (previous) (diff)

#14 @peterwilsoncc
6 months ago

#51517 was marked as a duplicate.

@hellofromTonya
6 months ago

Refreshed to fix failing test and phpcs errors.

This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.


6 months ago

#16 @TimothyBlynJacobs
6 months ago

@param int $taxonomy Current taxonomy object. should be a WP_Taxonomy right? It'd be nice to get this in a PR.

#17 @hellofromTonya
6 months ago

Good catch @TimothyBlynJacobs. Adding it. Will submit patch in a moment.

@hellofromTonya
6 months ago

Changes @param from int to WP_Taxonomy. Props Timothy.

#18 @antpb
6 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 50169:

Taxonomy: Add filter for post statuses when updating term count.

This adds a filter that allows $post_statuses to be modified in term count.

Props GunGeekATX, adamsilverstein, davecpage, nwjames, hellofromTonya, audrasjb, peterwilsoncc, TimothyBlynJacobs.
Fixes #38843.

#20 @SergeyBiryukov
6 months ago

In 50231:

Docs: Use typed array notation in the update_post_term_count_statuses filter DocBlock.

Follow-up to [50169].

See #38843.

Note: See TracTickets for help on using tickets.