I have been looking for an answer for the last couple of hours but could not find anything pointing me in the right direction. Some advice would be very much appreciated!

  • What I already have:
    • A custom post type 'stores'
    • A public query var using the 'query_vars' filter hook: 'state'.
    • Added the according rewrite rule:
      • Pattern: stores/state/([^/]+)/?$
      • Match: index.php?post_type=stores&state=$matches[1]
    • As 'state' is a custom meta field, I added a function hooking into pre_get_posts action hook doing all the query magic (creating meta_query etc) to display only the matching stores in the archive page.

That's all working quite well:

So if I enter www.myurl.com/stores/state/austria my archive page is only showing Austrian stores.

To filter the stores not only by url, the archive page includes a form to filter them. This is where I run into my problem: the form obviously only passes POST or GET data, e.g. like the following:


I can deal with this by adding a check in my pre_get_posts filter function:

if( isset( $_REQUEST['state'] ) || $query->get('state') != "" ) {

However, this is a little dirty as I would like wordpress to automatically redirect to the rewritten URL:


should be redirected to


the same way it is done if I enter a page or post ID and it redirects to the permalink.

Is there another action hook I have not discovered yet? How would I get wordpress to automatically redirect to the pretty permalink?

Thank you for every hint.


I figured out a solution myself. No javascript required, only another template function. URL building done manually.

The idea is to use the hook 'template_redirect', check if the requested page is the corresponding store archive page, then check for $_REQUEST vars there and build a new URL. Finally redirect to this URL.

function my_template_redirect() {
    if ( is_post_type_archive( 'stores' ) ) {
        if ( isset( $_REQUEST['state'] )  ) {
            $myurl = get_post_type_archive_link( 'stores' );
            if (substr($myurl, -1) != "/") $myurl .= "/";
            $myurl .= "state/";
            $myurl .= trim( sanitize_title_for_query( $_REQUEST['state'] ) ) . "/";
            exit( wp_redirect( $myurl ) ) ;
add_action( 'template_redirect', 'my_template_redirect' );

Works pretty well. Any corrections/better ideas are welcome of course.

  • 1
    Good. Just a tip if (substr($myurl, -1) != "/") $myurl .= "/"; $myurl .= "state/"; can be written as $myurl = trailingslashit($myurl) . '/state/'. However +1
    – gmazzap
    Sep 3 '13 at 10:22

Maybe this is not the best solution, nor the solution you are looking for, but this comes in my mind now, someone can maybe give you another.

I think in the page form you have something like this:

<select name="state">
   <option value="austria">Austria</option>

Using javascript it's easy send user to the pretty url.

(I put javascript here, you ensure put it in a separate js file and enqueue it with wp_enqueue_script)


jQuery().ready(function($) {
    var state = $(this).children('option:selected').val();
    window.location.href = window.location.href + '/' + state;
  • It quite certainly will work, but to be honest I think this also is a rather dirty 'solution' for two reasons:
    – mndmkr
    Sep 2 '13 at 15:42
  • The 2 reasons? :) Yes, as I said in first sentence, is not a good solution. Every solution that require js are dirty ones in wp. It's just a workaround until someone will answer you better. I admit rewrite rules are not my favourite WP argument. If can be useful, in a theme I've developed with similar situation, I've used js to hide things (using data- attribute) when items already there and when I need to load I used ajax. For browser with no js I let form submit and show the filtered items via $_POST var. @mndmkr
    – gmazzap
    Sep 2 '13 at 15:52
  • I'm sorry, I hit the enter key too early. My apologies. 1) The page won't work without javascript enabled. However, I could put in a fallback that relies on the GET/POST string. 2) The javascript does not rely on the WP functions / query vars, it is hard-coded. Changing the query vars would result in a disfunctional javascript. Probably could work around that as well by putting the javascript together dynamically. Ideally I would like a solution where the redirection and URL building is done by WP - as it is certainly capable of it. But thank you for you input anyway!
    – mndmkr
    Sep 3 '13 at 9:15
  • @mndmkr For the 1) you get solution. For 2) you can pass the right redirect url via wp_localize_script and it will works. But.. again, I'm not promoting my answer, as already said is more a workaround than a really solution.
    – gmazzap
    Sep 3 '13 at 9:20
  • Thank you for your comments, G.M. I just a found a solution using another WP hook - maybe it will be useful for you too!
    – mndmkr
    Sep 3 '13 at 9:46

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.