Custom Rewrite Rules

I am developing a plugin, the goal of which is to allow for a bookmark-able, pretty permalink search engine on a single page. To do so, I am using WP’s rewrite rules to allow for the searching/permalinks.

I need a structure as follows:
http://example.com/courses/nc/charlotte
Where /nc/ (or whatever is inputted/sent) would be accessible as a state variable later on, and the same for /charlotte/ as a city.

Read More

My code so far is as follows:
http://pastebin.com/PNHudKg5
(updated to include suggested code from below, which did not stop the query vars from being stripped).

Currently, the echo '<h2>'.$course->post_title.'</h2>'; works as it is outputted before the rest of WP loads. However, if I move the code from the query parse function to a shortcode, the query params /nc/charlotte disappear off of the url and are inaccessible by my shortcode.

Am I missing some function that will keep the query parameters so I can use them on page? It seems like when the page loads, it strips the url, but before the page loads, they are still accessible.

Or maybe I’m doing it wrong altogether? I’ve found so many different ways of using WP’s rewrite rules that my head is spinning. WP provides documentation on the functions, but not how to hook them all together to build something with them.

Also, I’m using Jan Fabry’s Rewrite Analyzer plugin. When I type in http://example.com/courses/nc/charlotte, I get the correct match in the window below. So I know I’ve got something right.

Updated info: the shortcode was just a temporary means of executing code on the page. I actually need the page to utilize it’s normal query in that it pulls the regular page content. The search results are supposed to be appended after the page content (hence the shortcode, or probably later – a page template).

Therefore, I cannot REPLACE the query, I am building a new one, and adding the matched posts to the page in the area designated (again, currently a shortcode in the content; later will be a page template).

My setup works awesome currently as I have it setup to use typical GET requests – like example.com/courses/?state=nc&city=charlotte

The above url gets posts with meta where custom fields (in this case, state and city) match the city and state in the GET request.

My goal is to move this functionality to WP Rewrite rules, like I mentioned above. Hope that makes it more clear.

Related posts

Leave a Reply

1 comment

  1. Within your rewrite() method add:

    add_rewrite_tag('%state%','([^&]+)');
    add_rewrite_tag('%city%','([^&]+)');
    

    That will keep the query_vars you added within the url, and prevent it from being stripped.