1
votes

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 result page is located at this url : http://www.domain.com/objects/

I want to create this kind of URL to point a listing of new objects : http://www.domain.com/objects/new

The rewritten query must be : http://www.domain.com/objects/?filter=new

I will then be able to use the $_GET['filter'] value to query my objects (meta_query)...

my problem

I've try to to this with this code:

add_rewrite_tag('%filter%','([^&]+)');
add_rewrite_rule('objects/new/','objects/?filter=new','top');

.htaccess looks then like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^objects/new/ /objects/?filter=new [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Unfortunately, when I try to reach the URL http://www.domain.com/objects/new I've got a 404 error :(

Debug bar show me this:

Request:
   objects/new

Query String:
   attachment=new

Matched Rewrite Rule:
   [^/]+/([^/]+)/?$

Matched Rewrite Query:
   attachment=new

Can someone tell me why Wordpress try to retrieve an attachment? Is something wrong in my rewrite rule or am I missing something?

Thanks in advance for your help!

3
Few Questions 1. is this working properly - domain.com/objects 2. domain.com/objects/filter/new - whether it will be ok for you ? - Pramod Kumar Sharma
Hi Pramod, I don't understand what you mean with these questions? domain.com/objects is an existing WP page, and domain.com/objects/filter/new is not a good solution for me since I don't want the "filter" in the final url (I want domain.com/objects/new) - MavBzh

3 Answers

4
votes

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'] ) ) { ... }
2
votes

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

enter image description here

0
votes

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]