admin管理员组

文章数量:1124556

We have some rewrite rules and would like to get the url parameters on that pages/feeds.

That does work on regular feeds, but not on those with a rewrite rule.

On regular feeds like /?somevalue=xyz I can use get_query_var('somevalue') and it results in xyz.

On rewritten pages like /?somevalue=xyz it is just empty.

We have first defined the query vars:

function add_get_val() { 
    global $wp; 
    $wp->add_query_var('somevalue'); 
}
add_action('init','add_get_val');

Then we did this rewrite rule:

function custom_rewrite_rule() {
    add_rewrite_rule('^rewrittenpage/([^/]+)/feed/?', 'index.php?custom_name=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rule');

And did define this query var:

function custom_query_var($query_vars) {
    $query_vars[] = 'custom_name';
    return $query_vars;
}
add_filter('query_vars', 'custom_query_var');

Last we redirected to the feed template with a query:

function custom_template_redirect() {
    global $wp_query;
    $custom_name = get_query_var('custom_name');

    if ($custom_name) {
        $wp_query = new WP_Query(array(
            'meta_query' => array(
                array(
                    'key' => '_custom', 
                    'value' => $custom_name,
                    'compare' => '=',
                ),
            ),
        ));
        load_template( STYLESHEETPATH . '/feed-rss2.php');  
    }
}
add_action('template_redirect', 'custom_template_redirect');

This works totally fine. But as already described: When I want to get my query vars (like somevalue) within the template on that rewritten feed, get_query_var('somevalue') is always empty.

How can I access any query vars within that rewritten template?

本文标签: queryGet URL parameters with rewrite rules