Make WordPress Core

Opened 11 months ago

Last modified 11 months ago

#58490 new defect (bug)

Make wp_update_term_count function site context aware for the multisite

Reported by: sathyapulse's profile sathyapulse Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch
Focuses: multisite Cc:

Description

During the import process, a performance problem arose because of term counting with each insert. However, this issue was reported and resolved with ticket #5377. Unfortunately, the solution is not compatible with multisite.

The syndication plugin causes delays in the syndication of multiple posts between sites in the multisite, primarily because of the term counting required during each insert.

Suppose you need to synchronise content across subsites and want to enhance the sync performance by turning off the term counting logic. In that case, you would follow these steps.

$post_list = [1, 2, 3];
switch_to_blog(2);
wp_defer_term_counting(true);
do_multiple_syncs( $post_list );
wp_defer_term_counting(false);
restore_current_blog();

We made a change by disabling term counting during each insertion and performing it after the inserts, as planned. However, this caused some problems. The menus on our website kept disappearing without any apparent reason, and we had to resave them to restore them manually.

Upon inspection, it was discovered that the wp_update_term_count function does not consider the multisite when the term counting is disabled https://github.com/WordPress/wordpress-develop/blob/6.2/src/wp-includes/taxonomy.php#L3464. For instance, in the sync job mentioned above, if the list of term IDs from subsite 2 is added for a recount in a static variable, the function incorrectly recounts it in the main site.

The term count was updated to 0 in the database. This is because the term ID doesn't exist on the main site or no posts have been assigned. It causes the check (https://github.com/WordPress/wordpress-develop/blob/6.2/src/wp-includes/nav-menu.php#L721-L725) to fail on the wp_get_nav_menu_items function and menus disappear.

Change History (4)

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


11 months ago

#2 @sathyapulse
11 months ago

Changing the order of the function calls in the description.

$post_list = [1, 2, 3];
wp_defer_term_counting(true);
switch_to_blog(2);
do_multiple_syncs( $post_list );
restore_current_blog();
wp_defer_term_counting(false);

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


11 months ago
#3

  • Keywords has-patch added

Updated wp_update_term_count() to keep track of current blog_id and loop through the correct sites as it processes deferred terms.

This was a collaboration with: @sathyapulse

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

#4 @ideag
11 months ago

  • Focuses multisite added

Worked with @sathyapulse on this one.

Note: See TracTickets for help on using tickets.