Avishay28
Avishay28

Reputation: 2456

Get posts belonging to three categories

I want to get posts that match three categories. For example: if I have three categories, named 1, 2, 3, I want to grab the posts that belong to 1 AND 2 AND 3, and only that posts. I found a way to make it with two categores:

$args = array(

        'category__and' => array(5739,50),
        'posts_per_page' => 10, 
        'orderby' => 'date'
);

But not three.

Thanks in advance.

Upvotes: 0

Views: 47

Answers (2)

Hardik Solanki
Hardik Solanki

Reputation: 3195

If you want to show posts from several categories

Then you can display it using following code :

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

If you want to show posts from several categories with AND condition

Then you can do it using following code:

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

If you want to show posts from several categories with OR condition

Then you can do it using following code:

$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

Upvotes: 3

Maha Dev
Maha Dev

Reputation: 3965

You can use WP_Query for getting posts from multiple categories like:

query_posts( array( 'category__and' => array(34,26,29), 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );

Here is the helping link:

http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Upvotes: 1

Related Questions