0

I am using this code to count number of Custom Post Type

$tax_terms = get_terms($tax, $args);
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name. '(' . $tax_term->count .')' .'</a></li>';
}

the code however is counting even deleted Post under the term. For example I already had 10 post under Term "Rayban" and I deleted 7 of them but the $tax_term->count still showing 10.

Can you please let me know why this is happening and how I can fix it?

Update:

Here is the Code I have for Tax Registry:

 function tax_sunglasses() {
    $labels = array(
        'name'                       => 'Sunglasses Brands',
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'sunglassesTax', array( 'sunglassesCPT' ), $args );
}
 add_action( 'init', 'tax_sunglasses', 0 );

Here is the Custom Post Type Registry:

function cpt_sunglasses() {

    $labels = array(
        'name'                => 'Sunglasses',
    );

    $args = array(
        'description'         => 'This Post Type Adds Sunglasses to Website',
        'labels'              => $labels,
        'supports'            => array( 'title', 'thumbnail', ),
        'taxonomies'          => array( 'sunglassesTax' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => get_template_directory_uri().'/img/sun.png',
        'rewrite'             => array( 'slug' => 'sunglasses' ),
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );
    register_post_type( 'sunglassesCPT', $args );
}
add_action( 'init', 'cpt_sunglasses', 0 );

And here is the code which I am using to generate the list of Terms and Counts( I have this list under page page-sunglasses.php)

<ul class="productList">
    <?php
        $tax = 'sunglassesTax';
        $args = array(
        'hide_empty' => false,
        'orderby' => 'name',
        'order' => 'ASC'
        );
        $tax_terms = get_terms($tax, $args);
        foreach ($tax_terms as $tax_term) {
        echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name. '(' . $tax_term->count .')' .'</a></li>';
        }
    ?>
</ul>
  • 2
    Your code seems fine, if it doesn't work there's something that prevent correct update of term count when a post is deleted, maybe a bad update_count_callback when taxonomy is registered or something messing up post status transition... or something else. Post the $args array you are using and your taxonomy and CPT registration code (you can skip labels for easier readability), but problem may be somewhere else. – gmazzap Oct 22 '14 at 19:47
  • Hi G.M. Thanks for hint, I just updated the post by adding CPT, Tax and Loop to get the list of Terms and Counts. Can you please take a look at them and let me know what is going on? once again thanks – Behseini Oct 22 '14 at 22:33
  • Just to add to @G.M. Comment. Don't you have any cache plugin installed. Have you cleared all caches – Pieter Goosen Oct 23 '14 at 10:28
  • Hi again Pieter :-), Don't you have any cache plugin installed not sure about this! Do I have to install anything on downloaded WP 4? – Behseini Oct 23 '14 at 16:44
  • all right I installed the WP Supe Cache plugin and using the Contents Tab I deleted all expired and all cashed! but still having same numbers! – Behseini Oct 23 '14 at 20:39

Your Answer

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

Browse other questions tagged or ask your own question.