Make WordPress Core

Opened 5 months ago

Closed 4 months ago

Last modified 3 weeks ago

#63562 closed defect (bug) (fixed)

Term counts should not be recalculated when a post transitions between statuses that are not counted

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-unit-tests has-patch
Focuses: performance Cc:

Description

This is similar to #42522 but subtly different.

When a post transitions from one status to another, term counts for the terms of the post are unnecessarily recalculated even when neither the old status or the new status are statuses which are included when counting terms. An example is when an autosave is first created.

In simpler terms, term counts for a post should only be recalculated when either its old or new status is publish. It just needs to respect other statuses added via the update_post_term_count_statuses filter too.

Change History (14)

This ticket was mentioned in PR #8970 on WordPress/wordpress-develop by @hbhalodia.


5 months ago
#1

  • Keywords has-patch added; needs-patch removed

Trac ticket: https://core.trac.wordpress.org/ticket/63562

  • Updated the function _update_term_count_on_transition_post_status which is hooked to transition_post_status action, and added condition to only recalculate when the new status or old status is publish.

#2 in reply to: ↑ description @sandeepdahiya
5 months ago

  • Keywords changes-requested added; has-patch removed

It just needs to respect other statuses added via the update_post_term_count_statuses filter too.

@hbhalodia I think the 'update_post_term_count_statuses' filter can be used to fetch other statuses as well -

	$countable_statuses = apply_filters( 'update_post_term_count_statuses', array( 'publish' ), null );

	if ( ! in_array( $new_status, $countable_statuses, true )
		&& ! in_array( $old_status, $countable_statuses, true )
	) {
		return;
	}

@mukesh27 commented on PR #8970:


5 months ago
#3

It just needs to respect other statuses added via the update_post_term_count_statuses filter too.

The implementation for filter is missing.

@hbhalodia commented on PR #8970:


5 months ago
#4

The implementation for filter is missing.

Yes, Thanks for the note, I am adding the implementation for the same.

#5 @hbhalodia
5 months ago

Hi @sandeepdahiya, Thanks for the note, Yes I am adding the implementation for the same.

@hbhalodia commented on PR #8970:


5 months ago
#6

Hi @mukeshpanchal27, I have implemented the fix to respect the filter, Let me know if that is correct approach or need to update with some other logic.

Thank You,

@mukesh27 commented on PR #8970:


5 months ago
#7

Let's add unit tests that validate the updated functionality.

@hbhalodia commented on PR #8970:


5 months ago
#8

Hi @mukeshpanchal27,

I reviewed this and looked into how we can accommodate the suggested test. However, it seems that the existing tests already cover the intended functionality:

The tests in this file already handle the scenario: https://github.com/WordPress/wordpress-develop/blob/trunk/tests/phpunit/tests/term/termCounts.php

For example, we've added a check to ensure that if both the old and new statuses are the same, the term count remains unchanged. This specific case is already covered here: https://github.com/WordPress/wordpress-develop/blob/ebace79f789958504e1ec27e6644fac79ebc3ee4/tests/phpunit/tests/term/termCounts.php#L172

The data provider for that test includes conditions where both statuses are the same, which ensures no change in the term count unless one of the statuses is publish.

Additionally, we already have a test for the update_post_term_count_statuses filter, which covers scenarios where a custom status is added:
https://github.com/WordPress/wordpress-develop/blob/ebace79f789958504e1ec27e6644fac79ebc3ee4/tests/phpunit/tests/term/termCounts.php#L242

So I am not sure if we should proceed to add the test, because I do not see any test specific to this function _update_term_count_on_transition_post_status added earlier as it seems test functionality covered by the mentioned file.

Let me know if we need to add a specific test for this?

Thank You,

#9 @johnbillion
5 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Owner set to johnbillion
  • Status changed from new to reviewing

#10 @johnbillion
5 months ago

  • Keywords has-patch added; changes-requested removed

#11 @peterwilsoncc
4 months ago

I've added a note to the effect on the pull request, I think the same unnecessarily counting takes place when both the old and new statuses included in the term count (when the update_post_term_count_statuses modifies the statuses included).

Let's cover that case while reducing useless recounts.

#12 @johnbillion
4 months ago

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

In 60510:

Posts, Post Types: Don't unnecessarily recount terms when a post transitions between statuses that don't require them to be recounted.

This accounts for transitions where:

  • Both the old and new statuses are not included in term counts.
  • Both the old and new statuses are included in term counts.

This results in term counts only being recalculated when a post transitions between a counted and an uncounted status.

If the terms of the post are changed then the term recounting is still handled by wp_update_term_count() inside wp_set_object_terms().

Props hbhalodia, johnbillion, peterwilsoncc, mukesh27.

Fixes #63562

This ticket was mentioned in Slack in #core-performance by johnbillion. View the logs.


7 weeks ago

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


3 weeks ago

Note: See TracTickets for help on using tickets.