0

I know how to filter posts in the admin view by taxonomy terms but in my case I'd like to be able to view posts based on whether they have a specific taxonomy (not its terms) attached.

For example: Post type is 'event'. There's two taxonomies - 'Regional' and 'International'. They both have their own terms and I can create two separate filters for them. What I'd like to have is a filter switch that would enable me to see events that are either 'Regional' or 'International'.

Could someone point me to the right direction? I'm fairly new to WP_Queries but I imagine I'd have to check if the post belongs to any of the terms of the taxonomy?

0

After a lot of fiddling I managed to get the functionality working.

First I add custom dropdown boxes for 'Regional' and 'International' using wp's action restrict_manage_posts. Then I set up a custom function that will be added to wp's filter parse_query.

In that function:

// type is the name of the select field 
if($_GET['type'] == 'Regional'){

  // get terms as WP_Term objects
  $regional_terms_objects = get_terms( array(
    'taxonomy' => 'regional',
    'hide_empty' => false,
  ));

  // convert them to an array of slugs
  $regional_terms_objects = array();
  foreach ($regional_terms_objects as $regional_terms_object){
    $regional_terms[] = $regional_terms_object->slug;
  }

  // filter the posts by all terms for taxonomy
  $tax_query = array(
    'taxonomy' => 'event-categories',
    'field' => 'slug',
    'terms' => $regional_terms
  );
}else{
   // the same stuff for 'International'
}
// add the query
$query->set('tax_query', $tax_query);

That way I can choose between 'Regional' and 'International' in the admin view and see only the events that belong to the chosen taxonomy. I hope someone can use that :)

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.