Wordpress simple rewrite rule not working -
i'm trying create rewrite rule in wordpress create direct pretty links search results.
i'm working custom post type called 'object'
my result page located @ url : http://www.domain.com/objects/
i want create kind of url point listing of new objects : http://www.domain.com/objects/new
the rewritten query must : http://www.domain.com/objects/?filter=new
i able use $_get['filter'] value query objects (meta_query)...
my problem
i've try to this code:
add_rewrite_tag('%filter%','([^&]+)'); add_rewrite_rule('objects/new/','objects/?filter=new','top');
.htaccess looks 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 try reach url http://www.domain.com/objects/new i've got 404 error :(
debug bar show me this:
request: objects/new query string: attachment=new matched rewrite rule: [^/]+/([^/]+)/?$ matched rewrite query: attachment=new
can tell me why wordpress try retrieve attachment? wrong in rewrite rule or missing something?
thanks in advance help!
ok, i've finnaly found way achieve this.
here code added 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 wordpress id of "/objects" page want reach rewritten url, and:
function custom_wp_querystring() { add_rewrite_tag('%filtre%','([^&]+)'); }
in order tell wordpress need use custom query var called "filtre".
this last function must added on init action:
add_action( 'init', 'custom_wp_querystring'); add_filter( 'generate_rewrite_rules', 'custom_rewrite' );
important thing know, in page template (the 1 id 27 in example), if want test query var, it's not possible kind of test:
if ( isset ( $_get['filtre'] ) ) { ... }
get vars 'hidden' due wordpress redirection, must do:
if ( isset( $wp_query->query_vars['filtre'] ) ) { ... }
Comments
Post a Comment