0

I was wondering if I can filter archive posts that are in a specific category, then apply another filter. For example, I have a featured category (where I display featured posts) and then I have my usual categories (like Personal, Blog, Fun). If i`m on /category/fun can I display all my posts from Fun category that are also in featured? I tried something, but this kinda shows me all posts from both categories...

<?php global $cat;
        $curr_cat = get_category( $cat );
        $cat_name = ( $curr_cat ) ? $curr_cat->cat_name : 'No Category Found!!';
        ?>
        <?php
        $my_query = new WP_Query('category_name=featured,'.$cat_name.'&showposts=8');
        while...
?>

Any thoughts?

1 Answer 1

1

You need a tax_query argument to WP_Query, if I am reading your question correctly.

$my_query = new WP_Query(
        array(
            'tax_query' => array(
        array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => array($featured_ID,$curr_cat->cat_ID),
                    'operator' => 'AND'
        )
         )
        ),
        'posts_per_page' =>8
    )
);

Untested, but I think that should be close.

Also, where does global $cat; come from? I don't remember seeing that as a Core global but maybe I'm wrong. If that isn't set your code will produce a very broken argument:

'category_name=featured,No Category Found!!&showposts=8'

That may be some of the problem you are seeing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.