Create a page without adding a page in the Database

I am trying to include a special Archive page with my theme, right now I have a PHP file with

Template Name: Archives Template at the top and I the user must create a blank archive POST PAGE and then select this as the template.

The content for creating the page is blank so nothing is added into the database for the page content. Is there anyway to make it where they do not have to do this?

3 s
3

To wit, this technically isn’t a ‘page’, however it’s a way to surface content using mod_rewrite. The goal here is to add a URI segment of foobar that will trigger a custom query, then include the specified template file.

Add a rewrite rule:

add_action('init', 'foo_add_rewrite_rule');
function foo_add_rewrite_rule(){
    add_rewrite_rule('^foobar?','index.php?is_foobar_page=1&post_type=custom_post_type','top');
    //Customize this query string - keep is_foobar_page=1 intact
}

See the WP_Query documentation for more information about customizing the query string.

Register a new query var:

add_action('query_vars','foo_set_query_var');
function foo_set_query_var($vars) {
    array_push($vars, 'is_foobar_page');
    return $vars;
}

Force WP to select your page template:

add_filter('template_include', 'foo_include_template', 1000, 1);
function foo_include_template($template){
    if(get_query_var('is_foobar_page')){
        $new_template = WP_CONTENT_DIR.'/themes/your-theme/template-name.php';
        if(file_exists($new_template))
            $template = $new_template;
    }
    return $template;
}

Flush the rewrites by visiting Settings->Permalinks, then visit http://yourdomain.com/foobar

Leave a Comment