WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 weeks ago

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

Change History (28)

@GunGeekATX
4 years ago

Proposed patch with filter

#1 @GunGeekATX
4 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
2 months ago

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

#6 @adamsilverstein
2 months ago

  • Milestone changed from Awaiting Review to 5.7

#7 @adamsilverstein
2 months ago

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

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


2 months ago

Trac ticket:

@nwjames
2 months ago

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

#9 @nwjames
2 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 weeks ago

#11 @hellofromTonya
6 weeks ago

  • Keywords needs-dev-note added

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

@audrasjb
6 weeks ago

patch refreshed against trunk

#12 @audrasjb
6 weeks ago

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

@hellofromTonya
6 weeks ago

Works: tested before and after applying patch 38843.2.diff

#13 @hellofromTonya
6 weeks 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 weeks ago by hellofromTonya (previous) (diff)

#14 @peterwilsoncc
5 weeks ago

#51517 was marked as a duplicate.

@hellofromTonya
4 weeks ago

Refreshed to fix failing test and phpcs errors.

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


4 weeks ago

#16 @TimothyBlynJacobs
4 weeks 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
4 weeks ago

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

@hellofromTonya
4 weeks ago

Changes @param from int to WP_Taxonomy. Props Timothy.

#18 @antpb
4 weeks 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
4 weeks 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.