WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type with a few custom taxonomies and I am using a piece of code to filter the posts based on the taxonomy on the main edit page using drop-down menus, which works great, but I have found that when I click onto a taxonomy's main page, and click on the number of posts that contains a specific term, it says 'trying to get property of non-object' and does not filter them. This is the code that is causing me issues:

function taxonomy_filter_post_type_request( $query ) {
  global $pagenow, $typenow;

  if ( 'edit.php' == $pagenow ) {
    $filters = get_object_taxonomies( $typenow );
      foreach ( $filters as $tax_slug ) {
        $var = &$query->query_vars[$tax_slug];
          if ( isset( $var ) ) {
           $term = get_term_by( 'id', $var, $tax_slug );
             $var = $term->slug;

add_filter( 'parse_query', 'taxonomy_filter_post_type_request' );

The line that reads $var = $term->slug; seems to be causing the problem, But if i replace:

  $var = $term->slug;


  $var = $term->slug;

Then it works when I click on the number of posts that contain a certain term, but the drop-down menu filters fail to work.

By looking at this, is it possible to make them both work?

share|improve this question

I believe that the problem is with this line:

$var = &$query->query_vars[$tax_slug];
  1. &$query is not necessary, simle $query should be enaught.
  2. $query->query->vars[$tax_slug] returns not an ID of term, but term's slug


$term = get_term_by( 'id', $var, $tax_slug );

should be changed to

$term = get_term_by( 'slug', $var, $tax_slug );

But is not necessary as you want to get a slug.

Further, pay attention. "Category" and "Post tag" taxonomies are not present as "category" and "tag", but are stored as "category_name" and "tag", so you have to transform $tax_slug accordingly inside your foreach loop. Eg:

$tax_slug_original = $tax_slug;
if( $tax_slug == 'category' ){
    $tax_slug = 'category_name';        
if( $tax_slug == 'post_tag' ){
    $tax_slug = 'tag';          
$var = $query->query_vars[$tax_slug];  
$tax_slug = $tax_slug_original;
share|improve this answer
Your right, $var = &$query->query_vars[$tax_slug]; is definitely the problem. When i do var_dump($var); i find that when using the dropdown to select a term, it returns the ID. When I click onto a taxonomy's main page, and click on the number of posts that contains a specific term, it returns the slug. If i leave $term = get_term_by( 'id', $var, $tax_slug ); then it works with the dropdown, as that returns an ID, but if i change 'id' to slug, it no longer works with the dropdown, but works when i click on the number of posts within a certain term, as that returns a slug... If that makes sense! – RoseCoder Sep 17 '13 at 19:18
get_term_by return $term object. That 'id' stays for that by. Reads "get term which id equals $term_slug" which is a nonsence. You are using that functin wrong. Have a look into docs: codex.wordpress.org/Function_Reference/get_term_by – david.binda Sep 17 '13 at 19:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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