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?


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.


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.

  • 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"). 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. Sep 3 '12 at 9:42
  • The solution with 'post_type=page&name=stores' is not working for me either. Had to use the approach from Jareds solution with the page IDs. Jun 7 '17 at 9:36
  • If we make changes in permalinks, won't it affect all the URLs including other custom post types and pages which may not have that particular field? May 6 '19 at 9:47

I found the answer on Wordpress' website and have tested the solution: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule#Using_Custom_Templates_with_custom_querystring

Create a Page Template

Create a page template for stores and apply it to a page.

$stores = get_query_var('stores');

Add a Rewrite Tag

In your functions.php add a rewrite tag, which lets Wordpress know to look for the specific tag

function custom_rewrite_tag() {
  add_rewrite_tag('%stores%', '([^&]+)');
add_action('init', 'custom_rewrite_tag', 10, 0);

Add a Rewrite Rule

Now to format the URL and point it to the index.php with the vars, enter the following in your functions.php. Note: You have to update the page_id to the page_id of the stores page (it's 12 in this example).

  function custom_rewrite_rule() {
  add_action('init', 'custom_rewrite_rule', 10, 0);

From there you can browse to /stores/south-dakota/ and the $stores variable in your page template will be "south-dakota".


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;


Inside you template, get the query variable using this

$state = get_query_var('state');

I know this is a very old post, however the process and the code is still relevant.

I have created a working example as suggested by Jarad (thank you!) that can be seen here... https://localartandcraft.uk/geo/ (notice I have used "geo" for page name instead of "stores").

Works great!!!

I am looking to take this further as a full blown Geo Locating Store finder for Woocommerce and WCFM and so would like to have the State / County plus the Town / City. So therefore needing to rewrite based on one or two variables dependant on depth level of location given.

So... either: https://localartandcraft.uk/geo/northamptonshire/ (gives listings for State /County)

Or: https://localartandcraft.uk/geo/northamptonshire/northampton (gives listings for Town / City)

Here is the current working code:

Page Template...

// query var for Geo Page Template - Dean Hurley - 07-05-2020
$geo = get_query_var('geo');
echo "Arts and Crafts in ".$geo;

functions.php file...

// Rewrite tag for Geo Page Template - Dean Hurley - 07-05-2020
function custom_rewrite_tag() {
  add_rewrite_tag('%geo%', '([^&]+)');
add_action('init', 'custom_rewrite_tag', 10, 0);

function custom_rewrite_rule() {
add_action('init', 'custom_rewrite_rule', 10, 0);

Any help greatly appreciated...

Dean Hurley - Datalynk.UK

  • post hijacking is frowned upon...
    – catbadger
    Feb 22 at 4:51

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.