WordPress.org

Ready to get started?Download WordPress

Forums

Custom Taxonomy Post Count Incorrect (11 posts)

  1. David Calhoun
    Member
    Posted 3 years ago #

    In a nutshell, my issue is that my custom taxonomy term post count is including published, draft and trashed posts.

    I did a little searching on this topic around the internets and it seemed like other people may have been having the same problem that I am having, but I was unable to find a resolution to it.

    I have a custom post type which has it's own custom taxonomy. I list these custom taxonomy terms in a <ul> that includes the term name and the count of posts in that term. It all seems to work great, until you delete/draft one of these custom posts. The number of posts in the term does not go down. It counts all post, trashed/drafted/pending review.

    <?php
    	//List terms in a given taxonomy
    	$taxonomy = 'industry';
    	$term_args=array(
    	  'hide_empty' => false,
    	  'orderby' => 'name',
    	  'order' => 'ASC'
    	);
    	$tax_terms = get_terms($taxonomy,$term_args);
    	?>
    	<ul>
    	<?php
    	foreach ($tax_terms as $tax_term) {
    	echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all vendors in the %s industry" ), $tax_term->name ) . '" ' . '>' . $tax_term->name . '<span class="count">' . $tax_term->count . '</span></a></li>';
    	}
    	?>
    	</ul>

    Anyone have any ideas on how to fix this?

  2. David Gard
    Member
    Posted 3 years ago #

    I once had an issue where one category was reporting the incorrect number of posts - I created a new category, filtered posts by the offending category and then group added them all to the new category before deleting the offending category (hope that makes sense?!) - problem solved.

    Have you tried looking in the database to ensure that the term_taxonomy table is reporting the correct number under count? If it is then you know it is a problem with your code, if the wrong number is reported then I'd suggest doing the above on 1 or two of the offending terms and seeing if that fixes it.

  3. duffcub
    Member
    Posted 3 years ago #

    I'm seeing this exact problem (as described in the original post).

    Checking in the wp_term_taxonomy table shows an incorrect count (too high) for certain terms, so I can only assume this count is not being decreased when posts are deleted.

    I've been trying to track down the relevant code, but as yet no luck - would be grateful for any pointers.

  4. David Calhoun
    Member
    Posted 3 years ago #

    @duffcub, I never was able to resolve this issue.

  5. duffcub
    Member
    Posted 3 years ago #

    Hi,

    After further digging, I think I have a workaround which while not exactly solving the problem, does add a function which corrects the count whenever posts are trashed.

    Add this to your functions.php file, and all should be good. I've tested it on my site and it appears to be working as expected.

    /*
     * Corrects count in term_taxonomy table for terms which are are not in published posts
     */
    
    add_action('edited_term_taxonomy','yoursite_edited_term_taxonomy',10,2);
    function yoursite_edited_term_taxonomy($term,$taxonomy) {
      global $wpdb;
      $sql = "UPDATE wp_term_taxonomy tt
            SET count =
            (SELECT count(p.ID) FROM  wp_term_relationships tr
            LEFT JOIN wp_posts p
            ON (p.ID = tr.object_id AND p.post_type = '[post_type]' AND p.post_status = 'publish')
            WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
            WHERE tt.taxonomy = '[taxonomy_name]'
        ";
      $wpdb->query($sql);
    }

    With thanks to http://wordpress.stackexchange.com/questions/3166/custom-taxonomies-incorrectly-counting-revisions

  6. Daniele Raimondi
    Member
    Posted 3 years ago #

    Thanks you all, that really helps me! But I've found a strange behaviour. My term count was 8, with 6 published and 2 pending posts classified under that terms. So I've made those steps:

    1) I've added duffcub code to my functions.php
    2) re-saved a pending post as pending (i.e. no status change)
    3) term count goes down by one (as expected)

    4) I've commented out duffcub code
    5) re-saved the other pending post as pending (i.e. no status change)
    6) term count goes down by one again (?!)

    It seems that resaving pending posts o changing their status to draft (or to published then to pending or draft again) make terms counts update. Then the question is: why terms counts get out of sync at some point in the past?

    Other details: probably those 2 post was changed to pending by my client, just before I update from wp 3.1 to wp 3.1.1. Maybe this count bug is resolved in wp 3.1.1?

  7. Daniele Raimondi
    Member
    Posted 3 years ago #

    @duffcub: Just made some mods to make your function more general:

    add_action('edited_term_taxonomy','yoursite_edited_term_taxonomy',10,2);
    function yoursite_edited_term_taxonomy($term,$taxonomy) {
      global $wpdb;
      $DB_prefix=$wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
      $sql = "UPDATE ".$DB_prefix."term_taxonomy tt
            SET count =
            (SELECT count(p.ID) FROM  ".$DB_prefix."term_relationships tr
            LEFT JOIN ".$DB_prefix."posts p
            ON (p.ID = tr.object_id AND p.post_type = '".get_post_type()."' AND p.post_status = 'publish')
            WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
            WHERE tt.taxonomy = '".$taxonomy->name."'
        ";
      $wpdb->query($sql);
    }

    NB: Don't work in "quick edit" mode cause get_post_type return FALSE

  8. Daniele Raimondi
    Member
    Posted 3 years ago #

    PLZ don't use my previous post. Here's a version that works both in quick edit and full edit mode

    add_action('edited_term_taxonomy','yoursite_edited_term_taxonomy',10,2);
    function yoursite_edited_term_taxonomy($term,$taxonomy) {
      global $wpdb,$post;
      //in quick edit mode, $post is an array()
      //in full edit mode $post is an object
      if ( is_array( $post ))
        $posttype=$post['post_type'];
      else
        $posttype=$post->post_type;
      if ($posttype) {
        $DB_prefix=$wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
        $sql = "UPDATE ".$DB_prefix."term_taxonomy tt
              SET count =
              (SELECT count(p.ID) FROM  ".$DB_prefix."term_relationships tr
              LEFT JOIN ".$DB_prefix."posts p
              ON (p.ID = tr.object_id AND p.post_type = '".$posttype."' AND p.post_status = 'publish')
              WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
              WHERE tt.taxonomy = '".$taxonomy->name."'
          ";
        $wpdb->query($sql);
      }
    }
  9. Ellis Benus
    Member
    Posted 3 years ago #

    Is anyone experiencing an issue where a taxonomy is registered to multiple custom post types, and the count is including the numbers from all the custom post types?

    For instance, I have 1 taxonomy Books tied to two Custom Post Types, Stuff and Crap.

    If you look at the taxonomy list under either of the custom post types, it shows the same number, the sum of both custom posts that are in the one taxonomy...

    Thoughts?

  10. neomatrix2
    Member
    Posted 2 years ago #

    @mad_max, can you help me work this code, I figure yoursite_edited_term_taxonomy is the Update Count Callback function ,

    if i register a taxonomy say like this and use your modified version of duffcub's code:

    register_taxonomy(
    'genre',
    array('post'),
    array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
    'update_count_callback' => 'yoursite_edited_term_taxonomy'
    )

    But it dsnt seem to work

    I have your code exactly like you provided in functions.php but it showing me incorrect count

    Kindly help me resolve it!!

  11. neomatrix2
    Member
    Posted 2 years ago #

    'update_count_callback' => '_update_post_term_count' worked for me

Topic Closed

This topic has been closed to new replies.

About this Topic