0

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.

2
  • Q: Could Multisite be a reason this is not working? The taxonomy and post are both registered through plugins. They were active on the blog but not the network. Test: I instead activated them on the blog. A: It still does not work. – Robert Andrews Oct 16 '18 at 10:53
  • First code returns this through xdebug: "Argument #2 should be an array." Subsequently: "Trying to get property of non-object." – Robert Andrews Oct 16 '18 at 11:00
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.