Filtering Posts by Multiple Taxonomy Terms such as in an Admin Post Listing?

I don’t have the time to actually write up a fully working example for you right now, but since you requested help in email I figured I would point you in the right direction.

As you may or may not know, this isn’t really an issue for the admin post listing; most all of that code should work fine. The issue is that currently WordPress does not have the ability to use query_posts()/get_posts()/WP_Query to filter on more than one taxonomy term without using hooks. (There is a very good chance that will change in v3.1; we can only hope!)

The solution is to use a 'posts_where' hook as illustrated here with the tax_terms_where() function written for that answer:

You’ll also need to modify the 'parse_query' hook to capture your filter values, something like this (I have not tested this, so there may be small syntax errors):

<?php
add_filter('parse_query','yoursite_parse_query');
function yoursite_parse_query($query) {
  global $pagenow;
  $qv = &$query->query_vars;
  if ($pagenow=='edit.php') {
    $tax_terms = array();
    if (!empty($qv['marka'])) 
      $tax_terms[] = "marka:{$qv['marka']}";
    if (!empty($qv['konu'])) 
      $tax_terms[] = "konu:{$qv['konu']}";
    if (count($tax_terms)) 
      $qv['tax_terms'] = implode(',',$tax_terms);
  }
}

The above assumes you have dropdowns with names of 'marka' and 'konu'. You’ll probably also need to tell WordPress to recognize them as query vars using an 'admin_init' hook. Again, I haven’t tested this so I’m hoping it works as written:

add_action('init','yoursite_init');
function yoursite_init() {
  global $wp;
  $wp->add_query_var('marka');
  $wp->add_query_var('konu');
}

That’s about it. Combine the knowledge of the three posts; this one, the one on admin lists, and the one on multiple taxonomy queries and I think you’ll get it done. You might even want to post your solution for others to learn from. Let me know if you get stuck.

Leave a Comment