0

I am trying to get custom URLs to load a specific template page, and I am mostly there, but my last query_var in the string refuses to load. Here is what is in my functions file:

function custom_query_vars_filter($vars) {
  $vars[] = 'issueloc';
  $vars[] .= 'issuearea';
  return $vars;
}
add_filter('query_vars', 'custom_query_vars_filter' );

function prefix_issues_rewrite_rule() {
    add_rewrite_rule( 'issues/([^/]+)/([^/]+)', 'index.php?ae_issues=$matches[1]&issueloc=$matches[2]', 'top' );
    add_rewrite_rule( 'issues/([^/]+)/([^/]+)/([^/]+)', 'index.php?ae_issues=$matches[1]&issueloc=$matches[2]&issuearea=$matches[3]', 'top' );
}

add_action( 'init', 'prefix_issues_rewrite_rule' );

function prefix_url_rewrite_templates() {

    if ( get_query_var( 'issueloc' ) ) {
        add_filter( 'template_include', function() {
            return get_template_directory() . '/taxonomy-ae_issues.php';
        });
    }

and when I load a page like mysiteaddress/issues/issueloc/issuearea, it will correctly load the template I want, and I will be able to pull the query_var for issueloc. But the query_var for issuearea is absent from the page.

If (on the other hand) I load a page like mysiteaddress/issues/issueloc/?issuearea=something, it will recognize my issuearea query_var (in addition to the others).

Is there something I am missing here? Why won't the last query_var load in the rewritten URL (which does load the correct template)? Is there something wrong with my second rewrite rule?

1 Answer 1

0

[] = not [] .= to append to an array.

function custom_query_vars_filter($vars) {
  $vars[] = 'ae_issues';
  $vars[] = 'issueloc';
  $vars[] = 'issuearea';
  return $vars;
}

Another trick, if you don't want to mess with a query is to change these vars to something like _issueloc & _issuearea. I found making custom queries as flags is nice for template redirects but not necessary to mess with the main query (unless you require it).


WORKING

An alternate might be to added using add_rewrite_tag().

add_action( 'init', function() {
    add_rewrite_tag( '%ae_issues%', '([^/]+)' );
    add_rewrite_tag( '%issueloc%', '([^/]+)' );
    add_rewrite_tag( '%issuearea%', '([^/]+)' );

    add_rewrite_rule( '^issues/([^/]+)/([^/]+)/([^/]*)', 'index.php?ae_issues=$matches[1]&issueloc=$matches[2]&issuearea=$matches[3]', 'top' );
    add_rewrite_rule( '^issues/([^/]+)/([^/]+)', 'index.php?ae_issues=$matches[1]&issueloc=$matches[2]', 'top' );
} );

add_filter('template_include', function($template){

    global $wp_query;
    if( isset($wp_query->query_vars['ae_issues'])) {
        echo "<pre>";
        print_r($wp_query->query_vars);
        wp_die();
    }
    return $template;

});
3
  • Thanks for the response, but removing the . makes no difference for my problem, it stays exactly the same (even after reloading permalinks)
    – Stephen
    Commented Nov 21, 2016 at 22:53
  • This update is working for me. Accessing the vars from the global $wp_query;
    – jgraup
    Commented Nov 22, 2016 at 1:31
  • That last update got it working ( I think it was the add_rewrite_tags or the changing order of the rewrite rules), thanks!
    – Stephen
    Commented Nov 22, 2016 at 3:12

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.