Make WordPress Core

Opened 7 years ago

Closed 3 years ago

Last modified 3 years ago

#38843 closed enhancement (fixed)

Add filter for post statuses in _update_post_term_count()

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

Change History (28)

@GunGeekATX
7 years ago

Proposed patch with filter

#1 @GunGeekATX
7 years ago

  • Keywords has-patch added

#2 @adamsilverstein
6 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.


6 years ago

#5 @adamsilverstein
3 years ago

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

#6 @adamsilverstein
3 years ago

  • Milestone changed from Awaiting Review to 5.7

#7 @adamsilverstein
3 years ago

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

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


3 years ago
#8

Trac ticket:

@nwjames
3 years ago

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

#9 @nwjames
3 years 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.


3 years ago

#11 @hellofromTonya
3 years ago

  • Keywords needs-dev-note added

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

@audrasjb
3 years ago

patch refreshed against trunk

#12 @audrasjb
3 years ago

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

@hellofromTonya
3 years ago

Works: tested before and after applying patch 38843.2.diff

#13 @hellofromTonya
3 years 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 ) {
                        return array( 'future' );
                        $statuses[] = 'draft';
                        $statuses[] = 'future';
                        $statuses[] = 'pending';
                }

                return $statuses;
        },
        10,
        2
);

Marking this ticket ready as a commit candidate.

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

#14 @peterwilsoncc
3 years ago

#51517 was marked as a duplicate.

@hellofromTonya
3 years ago

Refreshed to fix failing test and phpcs errors.

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


3 years ago

#16 @TimothyBlynJacobs
3 years 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
3 years ago

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

@hellofromTonya
3 years ago

Changes @param from int to WP_Taxonomy. Props Timothy.

#18 @antpb
3 years 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
3 years 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.