Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom page which uses a template called "Stores" http://example.com/stores/. This page will have a list of states and/or companies as categories. For this system I have a custom post type "store" which can be assigned to a single category (either state or company),

Each store is not a specific page, but contact information which is displayed in a sort of phone-book style layout for states/companies.

The problem I have now is, how can I allow my "Stores" template accept URL requests from http://example.com/stores/california/ (without throwing 404 error)?

California in this sense is not a page, but a query. I also do not want to manually create a page for each of these categories (As it is pulled from database, and automatic). I could use ?state=california for this purpose, but I would like to learn the proper way to allow this URL within wordpress.

Is the correct way to edit htaccess file manually? Wouldn't that be overwritten if you edit the permalink structure? How would I format the htaccess?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Create a Page Template

Add a new page and give it the slug stores

Add A Public Query Variable

add_filter('query_vars', 'add_state_var', 0, 1);
function add_state_var($vars){
    $vars[] = 'state';
    return $vars;
}

Add a rewrite rule

This will direct your request to your stores page.

add_rewrite_rule('^stores/([^/]*)/?','index.php?post_type=page&name=stores&state=$matches[1]','top');

Within your Template

You can access your state variable as follows:

 $state = get_query_var('state');

Flush your Permalinks

Visit Settings->Permalinks to flush your permalinks and add your rewrite rule to the stack.

share|improve this answer
    
Sorry about the delay. This code is working, but there is a problem that I can't identify with the wiki. Now when I go to stores/alabama instead of getting a 404 page it correctly loads the Store page. But, it redirects me back to /stores/, and there is no sign of the query variable "state" (even dumped the $wp_query in my template, nothing about alabama). Why is it redirecting back to /state/? I flushed permalinks (thus the URL is still "working"). –  RadGH Sep 3 '12 at 9:22
1  
Update: If I remove post_type=page&name=stores then I arrive at the landing page, however the query variable "state" does exist. The redirect is removing the query var, although I am still not sure why specifying the post type is causing a redirect in the first place. –  RadGH Sep 3 '12 at 9:42

I was also trying to do the same and finally did it in following way.

Add the following code to your functions.php inside your theme.

add_filter('query_vars', 'add_state_var', 0, 1);
function add_state_var($vars){
    $vars[] = 'state';
    return $vars;
}

add_rewrite_rule('^stores/([^/]+)/?$','index.php?pagename=stores&state=$matches[1]','top');

Inside you template, get the query variable using this

$state = get_query_var('state');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.