• Resolved alexs

    (@alexsmoli)


    Hi guys,

    I’m sure that there is a really simple answer, but I can not seem to find the correct one.

    Imagine I have 3 categories and I would like to get the number only for the posts which apply to cat 1 and 2 at the same time.

    How do I do it?

    My code is:

    <?php
    $mycats=get_categories ('include=1');
    echo 'de'. $mycats[0]->category_count;
    ?>

    It should not display the number of posts of cat 1 and cat 2, but only of the ones which are in 1 AND 2 all together.

    Help is very much appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try a WP_Query:

    $count_query = new WP_Query( array(
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'tax_query'      => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'terms'    => array( 1 ),
            ),
            array(
                'taxonomy' => 'category',
                'terms'    => array( 2 ),
            ),
        )
    ) );
    
    echo $count_query->post_count;

    Thread Starter alexs

    (@alexsmoli)

    Perfect! Thank you. Already on live-system ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_category display posts which apply to 2 categories’ is closed to new replies.