0

I have a query_var set up for a search results page where my query_var is a taxonomy id used in a WP_Query loop, like:

~/results/?my_taxoqueryvar=123

I would like to implement a rewrite rule so that I can rewrite this so that it's a SEO friendlier, like:

~/results/my-taxo-slug/ (where 'my-taxo-slug' is the corresponding slug for the taxonomy with ID 123.

I also want to be able to access my query var (i.e. $qv_exp_type = get_query_var('my_taxoqueryvar');

This is what I have so far, but it doesn't appear to be working

// Add Query Var
function my_queryvar_params( $query_v ) {
    $query_v[] = "my_taxoqueryvar";
    return $query_v;
};
add_filter('query_vars',  'my_queryvar_params');


// Add Endpoint
function my_endpoint(){
    add_rewrite_endpoint( 'my_taxoqueryvar', EP_PAGES );
}
add_action( 'init', 'my_endpoint' );

function  my__rewrite_rule() {
    add_rewrite_rule(
        'results/([a-z]+)/?$',
        'index.php?pagename=results&my_taxoqueryvar=$matches[1]',
        'top'
    );
}
add_action('init',  'my_rewrite_rule' );

HUGE thanks in advance!

0

Your rewrite rule and query vars filter is wrong. Below is an example based on your query var my_taxoqueryvar

function add_query_vars_filter( $vars ){
    $vars[] = "my_taxoqueryvar";
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

add_action('init', 'myplugin_rewrite_rule' );
function myplugin_rewrite_rule() {
    add_rewrite_rule(
        'results/([a-z]+)/?$',
        'index.php?pagename=results&my_taxoqueryvar=$matches[1]',
        'top'
    );
}
  • Thanks @Tunji. I've updated the code in my question. Unfortunately though, the rewrite still doesn't appear to be taking effect (I've flushed rewrites). – Ryan Dorn Dec 21 '16 at 15:44

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.