Wordpress: How to count post in a category. Help needed with the second function to count multiple categories

Viewed 22

This function can count the number of posts in a category.

//working code
add_shortcode( 'cat_count2', 'cat_count2' );
function cat_count2( $atts ) {

$atts = shortcode_atts( array(
  'category' => null,
  'type' => null
), $atts );

$term = get_term_by( 'slug', $atts['category'], 'category');
$tpt = $atts['type'];


return ( isset( $term->count ) ) ? $term->count : 0;
}

Festivals ([cat_count2 category="en-festivals"])


I need help with a function that can count and add the number of posts in multiple categories. Here is the start of a function. Any help writing something is greatly appreciated.

add_shortcode( 'my_post_count', 'my_post_count_func' );
function my_post_count_func($tax, $cat1, $cat2) {
    $yay = array(
        'tax_query' => array(
        'relation' => 'AND',
            array(
                'taxonomy' => $tax,
                'field' => 'term_taxonomy_id',
                'terms' => array( $cat1 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => $tax,
                'field' => 'term_taxonomy_id',
                'terms' => array( $cat2 ),
                'operator' => 'IN'
            ),
        )
    
    );

    return ( isset( $yay->count ) ) ? $yay->count : 0;

}

January ([my_post_count categories="en-events,en-january"])

0 Answers
Related