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]
- Pattern:
- As
'state'
is a custom meta field, I added a function hooking intopre_get_posts
action hook doing all the query magic (creatingmeta_query
etc) to display only the matchingstores
in the archive page.
- A custom post type
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:
www.myurl.com/stores/?state=austria
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:
www.myurl.com/stores/?state=austria
should be redirected to
www.myurl.com/stores/state/austria
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.