Alex Robert
Alex Robert

Reputation: 103

WP get all posts from multiple combined category (Advanced)

hello i want to get all available posts from multiple category combination.

so my WP_Query Args would be like

$args = array(
'category_name' => 'slug_1+slug_2,slug_1+slug3'
);

But this does not work.

So the combination would be: get me all the posts from (cat1 and cat2) or (cat1 and cat3) in one query, because i need pagination.

UPDATE 1

To bee more specific i have a filter for the post to filter the categories that they have you can select multiple categories. the posts all have a default category assigned ex: Job Openings and in the filter I select another/multiple categories witch are also assigned to the posts.

so :

$args1 = array(
    'category_name' => 'slug_1+slug_2'
);
// or
$args2 = array(
    'category_name' => 'slug_1+slug_3'
);
//final arg would be the two combined with and or separator ','
$args_final = array(
    'category_name' => '$args1,$args2'
);

how is this possible using one wp_Query args. Thanks.

Upvotes: 1

Views: 2327

Answers (2)

zakhefron
zakhefron

Reputation: 1443

$query = new WP_Query( array( 'category_name' => 'cat1,cat2' ) );

Wordpress Codex

UPDATE

I hope merging two separate WP query result will solve this. Try something like below

$query1 = new WP_Query(array( 'category_name' => 'slug_1+slug_2' ));
$query2 = new WP_Query(array( 'category_name' => 'slug_1+slug_3' ));

$getId = array_merge($query1->posts,$query2->posts);

$finalQuery = new WP_Query(array('post__in' => $getId));

Upvotes: 5

kuldip Makadiya
kuldip Makadiya

Reputation: 715

Try to make your WP query like this:

$query = new WP_Query( 'category_name=staff,news' );

or make the query with category id:

$query = new WP_Query( 'cat=2,6,17,38' );

Have a look over the following link, it must help you: http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters

Upvotes: 0

Related Questions