Skip to main content
2 of 2
added 21 characters in body
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

filter on get_posts efficiently

I have posts with the following categories:

A category that is a year(2017,2018...2022), a category that is type:(worldwide,Europe, local)

I am trying to give users in the frontend advanced search ability they have two multiselect dropdown, they can pick a year or several. and a type. (both of these are technically categories)

For example select post from cat 2017 or cat 2018 AND that are (cat=Europe or cat=local)`

I am having trouble mixing the "and" and "or" efficiently... in get_posts.

For instance I can do something like:

$posts = get_posts(array( 'category__and' => array(1,10) ));  // assuming 10=category Europe, 1 =category 2016,

So I just do a loop like

Foreach $year in list of years:
  foreach $type in list of types
     get_posts(array( 'category__and' => array($year,$type) ));

Is there a more efficient way to combine my conditions?

Thanks

user206904
  • 181
  • 2
  • 8