0

Does anyone know if it is possible to have multiple arrays within query_posts if so how?

I'm looking to query posts that are in category 1 & 2. But also posts that are in 1 & 3 and 1 & 4.

So the posts MUST be in category 1 as well as one of the other categories.

So the category__and would be where I've started which allows me to query posts both in 1 & 2 but how would I then progress to add the others 3,4 etc? as adding them 1,2,3,4,5 will only show posts in all 5 categories correct?

Here is what I have so far:

<?php                                   
    query_posts( 
        array( 'category__and' => array(1,2), 
        'posts_per_page' => 5, 
        'orderby' => 'date', 
              ) );
    while (have_posts()) : the_post();  
?>

Is it possible to put an array of arrays to perform what I have asked?

1 Answer 1

1

This should be possible with the tax_query parameter.

query_posts(
    array(
        posts_per_page => 5,
        tax_query => array(
            relation => 'OR',
            array(
                'taxonomy' => 'category',
                'operator' => 'IN',
                'field' => 'id',
                'terms' => array( 1, 2 ),
            ),
            array(
                'taxonomy' => 'category',
                'operator' => 'IN',
                'field' => 'id',
                'terms' => array( 1, 3 ),
            ),
            array(
                'taxonomy' => 'category',
                'operator' => 'IN',
                'field' => 'id',
                'terms' => array( 1, 4 ),
            ),
        ),
    )
);

See the documentation for any additional details: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Sign up to request clarification or add additional context in comments.

1 Comment

Hi that's great, will that show posts that are in both 1 & 2 not just posts that are in 1.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.