I\'m trying to create a rewrite rule in Wordpress to create direct pretty links to search results.
I\'m working with a custom post type called \'object\'
My resu
OK, I've finnaly found a way to achieve this.
Here is the code I added to my theme:
function custom_rewrite( $wp_rewrite ) {
$feed_rules = array(
'objects/(.+)' => 'index.php?page_id=27&filtre=' . $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
where 27 is the Wordpress ID of the "/objects" page I want to reach with the rewritten URL, and:
function custom_wp_querystring() {
add_rewrite_tag('%filtre%','([^&]+)');
}
In order to tell Wordpress I need to use a custom query var called "filtre".
This last function must be added on init action:
add_action( 'init', 'custom_wp_querystring');
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );
Important thing to know, in my page template (the one with ID 27 in my example), if I want to test the query var, it's not possible to do that kind of test:
if ( isset ( $_GET['filtre'] ) ) { ... }
GET vars are 'hidden' due to Wordpress redirection, you must then do:
if ( isset( $wp_query->query_vars['filtre'] ) ) { ... }
Use this plugin Rewrite plugin it will help you to analysize which rule is working when you hit the url..you can create new rewrite rule and can check here itself whether they are working accordingly or not.
There is no need to write anything in wordpress because wordpress itself has a wp_rewrite class which manages all the rewrite rules.
Try with several rewrite rules and set the rule priority accordingly. because the rule mentioned or listed above in the list will match first if a match found above then wordpress will ignore the below listed rules.
I have used this plugin and its work for me the way i want. Attaching the screenshot here
The rewrite rule needs to be placed as
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^objects/new(.*)$ /objects/?filter=new [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]