0

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 );
            if($var){
             $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:

if($var){
  $var = $term->slug;
}

to:

if(is_array($var)){
  $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?

1 Answer 1

1

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

So

$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;
2
  • 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
    Commented Sep 17, 2013 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 Commented Sep 17, 2013 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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