Aleš Sýkora wrote

Taxonomy Post Count for Custom Post Type

16. 5. 2019 in

Add this snippet to your functions.php or create your own plugin:

function my_taxonomy_posts_count_func( $atts ) {
    extract(shortcode_atts(array(
    'post_type' => 'post',
    ), $atts));
    global $WP_Views;
    $term = $WP_Views->get_current_taxonomy_term();
    
    $args = array(
    'post_type'=> $post_type,
    'tax_query' => array(
    array(
    'taxonomy' =>$term->taxonomy,
    'field' => 'id',
    'terms' => $term->term_id,
    )
    ),
    'order' => 'ASC',
    'posts_per_page' => -1,
    'suppress_filters' =>0,
    );
    $posts = get_posts( $args );
    $res=0;
    if($posts)
    {
    $res = count($posts);
    }
    return $res;
    
    }
    
add_shortcode( 'my-taxonomy-posts-count', 'my_taxonomy_posts_count_func' );

Add this shortcode to your view:

[my-taxonomy-posts-count post_type="jobs"]

And change the “jobs” to your CPT slug.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Display only first image from multi value field in toolset
22. 7. 2020 in
I am using a custom field called “product images” where I upload images for my custom post type Coffee machines. When I want to show the image in the archive then, I just want to display the first Image. Not all of them. What happen is that toolset field automatically display all images from the […]
Read More
Aleš Sýkora wrote
Trigger js event after ajax finished
2. 7. 2019 in
There's a way to trigger a javascript callback after ajax event. The way how to do that is easy.
Read More
Aleš Sýkora wrote
Remove Archive Title prefix in WordPress
28. 5. 2021 in
Put this code in code snippets plugin.
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram