How to update incorrect post count in taxonomy?

I have imported a large amount of content using WPAllImport, all to a custom post type called "article" ("Articles") and all organised by a custom taxonomy type called "source" ("Sources").

However, on the edit-tags.php page for the Sources taxonomy listing, the Articles post counts are all inaccurate.

There is one term which shows as only having three Articles against it but, on the post index for Articles with that Source, it clearly has 1,997.

How can I fix all the counts?

I have tried to use wp_update_term_count_now

I tried ...

$update_taxonomy = 'source';
$get_terms_args = array(
    'taxonomy' = $update_taxonomy,
    'fields' = 'ids',
    'hide_empty' = false,
    );
$update_terms = get_terms($get_terms_args);
wp_update_term_count_now($update_terms, $update_taxonomy);

(via)

and the kitchen sink ...

$taxonomies = get_taxonomies();
foreach( $taxonomies as $taxonomy ) {
$args = array(
'hide_empty' = 0,
'fields' = 'ids'
);
$terms = get_terms( $taxonomy, $args );
if( is_array( $terms )  !empty( $terms ) ) wp_update_term_count_now( $terms,     $taxonomy );
}

(via)

But neither seems to do anything.

I put the code in a header.php theme file just to execute.

Topic terms count Wordpress

Category Web


It only took 3.5-years for someone to be put into the position of @robert-andrews to chase down the post count problem. While having the same problem (and misinterpretation), I ran across your post yesterday in my search for answers. Nothing good really turned up. So, I was forced to trace the code and take the long way home in figuring out what's going on. (Though there are other related posts here and here that touch on this subject, there is no "good" answer on the StackExchange family of sites or StackOverflow.)

You'll notice taxonomies generally appear under the admin menu of the post type to which they have been registered. That's important to note because wp-admin/edit-tags.php only shows post counts of the type under which the page is nested. (Hint: look in the URL, and you'll see the slug of that post type.)

Next, my tracing took me a private function _update_post_term_count(). What I found is that the count is stored in the database, not calculated dynamically. In other words, no results will be seen simply by doing a bit of coding and refreshing the page: a post associated with the taxonomy must be saved in order to manifest any results. (What a silly thing to do considering the limited number of times the count is accessed and the fact that post counts are dynamic in the admin table view counts.)

Anyway, back to _update_post_term_count(): it will only save posts of the specific post type that have published status. This was particularly distressing to me because I have a custom post status for a custom post type that is categorized by a custom taxonomy. Well, the update_post_term_count_statuses hook is what solves that problem. Here's a bit of shorthand of how I solved my problem of counting posts appearing in my CPT admin table "All" view:

add_filter('update_post_term_count_statuses', function($post_statuses, $taxonomy) {
    // Intercept
    if($taxonomy->name === 'mort-custom-taxonomy') {
        foreach(array(
            // Some post statuses to be included in the count ...
            'mort-custom-post-status',
            'draft',
            'pending',
            'private'
        ) as $v) {
            // Check to ensure another call hasn't already added the status.
            if(!in_array($v, $post_statuses)) {
                $post_statuses[] = $v;
            }
        }
    }
    // Return
    return $post_statuses;
}, 10, 2);

Here's how the filter above would be called: _update_post_term_count() is executed by wp_update_term_count_now() when wp_update_term_count() is put into effect by wp_set_object_terms() and _update_term_count_on_transition_post_status() at the time a post is created/updated/published.

One final important note so you don't drive yourself crazy. After implementing the filter, the count of a term will not be updated until (as you might have guessed) a post related to that term is created/updated/published.


The question arose out of misinterpretation...

On the taxonomy list in question, the Count number was inaccurate - but only in respect of one of the post types to which a term was attached.

What was not clear to me was that the Count column is an aggregate count, displaying the count of ANY post objects attached to the corresponding term, even when the context of the taxonomy listing page was clearly clicked through from a specific post type sub-menu.

It is likely that the above code to fix post counts worked successfully.

The answer here lays in explaining what was going on.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.