0

Hi,

I am on functions.php and I want to add this filter:

function graphene_alter_the_query( $request ) {
    $dummy_query = new WP_Query(); 
    $dummy_query->parse_query( $request );
    if (!is_category('25')) {
     $request['gdsr_sort'] = 'rating';
    }
    else {
        $request['gdsr_sort'] = 'comment_count';
    }
    return $request;
}
add_filter( 'request', 'graphene_alter_the_query' );

the filter should do the same thing for all categories except cat 25 but seems like it cant tell it apart like if the IF condition is not detecting the category so I tried to debug it by looking at the values in WP_Query and I get this:

$myquery = new WP_Query(); print_r($myquery);

WP_Query Object ( [query] => Array ( [category_name] => 'my cat' ) [query_vars] => Array ( [category_name] => 'my cat' [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 'my cat' ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => 'my cat' ) [field] => slug ) ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_favicon] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 6d85cdf18f3e6a58a470a3002d5807fc [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => 

but both tag_id and cat appear empty. Why is that?

Thank you.

3
  • 1
    If I'm reading this answer (wordpress.stackexchange.com/a/21378/16121) right, it looks like is_category() may not be available to you. Does replacing is_category( 25 ) with $dummy_query->is_category( 25 ) do what you want it to?
    – Pat J
    Apr 11 at 22:40
  • 1
    it does! Thank you!
    – Cain Nuke
    Apr 11 at 22:45
  • OK. I'm going to add that as an answer in case someone else is having a similar problem.
    – Pat J
    Apr 12 at 2:08

1 Answer 1

2

As pointed out in this answer, using the request filter means that some (if not all) of WordPress's is_* functions are unavailable. Use the corresponding member functions of your new WP_Query instead, as they should be available.

function graphene_alter_the_query( $request ) {
    $dummy_query = new WP_Query(); 
    $dummy_query->parse_query( $request );
    if ( ! $dummy_query->is_category( 25 )) {
     $request['gdsr_sort'] = 'rating';
    } else {
        $request['gdsr_sort'] = 'comment_count';
    }
    return $request;
}
add_filter( 'request', 'graphene_alter_the_query' );

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.