-1

For blog post page, I have about 100 posts-categories. If I select 2 or 3 or more categories out of them randomly, for example category A and category B, I hope the blog page can only show posts belonging to A and B at same time.

The following code can only show posts belonging to random categories selected by admin under "or" condition. If I select 2 categories randomly, the blog page shows all the posts belonging to any of the selected categories.

$args = Array(
    'post_type'      => $post_type,
    'post_status'    => 'publish',
    'posts_per_page' => $ppp,
    'paged'          => $page
);
$posts = new WP_Query( $args );

How do I update the code to show posts belonging to all the selected post categories at same time?

  • Welcom to WPSE. Just a tip, if you add code to your question, highlight your code in the editor and click on the {} sigh above the editor, this will add your code in a code block and make it readable :-). To get you started, please visit How to Ask, help center and take a tour to learn how the SE sites operate and how to get the maximum out of them. :-) – Pieter Goosen Mar 9 '15 at 4:17
0

To get posts that belongs to all selected categories, you can make use of the category__and parameter.

Example:

You can the following to your query arguments, this will only display posts that are in category 1 AND 2

'category__and' => array( 1, 2 ),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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