[wp-trac] [WordPress Trac] #40351: Term post re-counts scale poorly, are common and difficult to avoid
WordPress Trac
noreply at wordpress.org
Thu Oct 22 13:05:00 UTC 2020
#40351: Term post re-counts scale poorly, are common and difficult to avoid
-------------------------------------------------+-------------------------
Reporter: mattoperry | Owner: whyisjake
Type: enhancement | Status: reopened
Priority: normal | Milestone: 5.6
Component: Taxonomy | Version: 4.8
Severity: normal | Resolution:
Keywords: needs-dev-note has-patch has-unit- | Focuses:
tests | performance
-------------------------------------------------+-------------------------
Comment (by Chouby):
In Polylang we heavily rely on taxonomies, including taxonomy for terms to
create languages and translation groups for terms. However [49141]
introduces an issue in counting for these taxonomies.
I wrote 2 simple tests to explain what we are doing.
In this first test, we automatically create a term in a taxomomy
"term_taxonomy" when we create a tag. This test passes with WordPress 5.6
beta 1:
{{{#!php
function test_wp_set_object_terms_hooked_to_create_term() {
$args['update_count_callback'] = '_update_generic_term_count';
register_taxonomy( 'term_taxonomy', 'term', $args );
add_action( 'create_term', function( $term_id, $tt_id, $taxonomy )
{
if ( 'post_tag' === $taxonomy ) {
$term = wp_insert_term( uniqid(), 'term_taxonomy'
);
$lang = $term['term_id'];
wp_set_object_terms( $term_id, $lang,
'term_taxonomy' );
}
}, 10, 3 );
$tag_id = $this->factory->tag->create();
$terms = wp_get_object_terms( $tag_id, 'term_taxonomy' );
$this->assertCount( 1, $terms ); // Check that a term has been
assigned to the tag.
$term = reset( $terms );
$this->assertEquals( 1, $term->count ); // Check the term count.
}
}}}
In this second test, we do the same but the tag is created by
`wp_insert_post()`. This test passes with WordPress 5.5 but fails wiht 5.6
beta 1:
{{{#!php
<?php
function
test_wp_set_object_terms_hooked_to_create_term_in_wp_insert_post() {
$args['update_count_callback'] = '_update_generic_term_count';
register_taxonomy( 'term_taxonomy', 'term', $args );
add_action( 'create_term', function( $term_id, $tt_id, $taxonomy )
{
if ( 'post_tag' === $taxonomy ) {
$term = wp_insert_term( uniqid(), 'term_taxonomy'
);
$lang = $term['term_id'];
wp_set_object_terms( $term_id, $lang,
'term_taxonomy' );
}
}, 10, 3 );
$post_id = $this->factory->post->create( array( 'tags_input' =>
array( 'my_tag' ) ) );
$tags = wp_get_post_tags( $post_id );
$tag = reset( $tags );
$terms = wp_get_object_terms( $tag->term_id, 'term_taxonomy' );
$this->assertCount( 1, $terms ); // Check that a term has been
assigned to the tag.
$term = reset( $terms );
$this->assertEquals( 1, $term->count ); // Check the term count,
fails in WP 5.6.
}
}}}
The issue is caused by the call to `_wp_prevent_term_counting()` by
`wp_insert_post()`. It's ok for all the terms assigned directly by this
function, but it breaks the counting of terms assigned in functions hooked
to actions fired while the counting is prevented.
I am not sure it would fix all issues but one possible solution for
Polylang would be to make `_wp_prevent_term_counting()` taxonomy
dependent. That the term counting would be prevented only for taxonomies
handled directly by `wp_insert_post()` instead of all taxonomies at the
same time.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40351#comment:42>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list