[wp-hackers] any idea why this custom rewrite isnt working?

Chris McCoy chris at lod.com
Thu May 17 01:58:02 UTC 2012


The two tags being used, are in separate posts.

So ?tag=tag1,tag2 works

Just not with the -


Will try using pre_get_posts



-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com
[mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Dion Hulse
(dd32)
Sent: Wednesday, May 16, 2012 8:48 PM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] any idea why this custom rewrite isnt working?

Short answer is:

// multiple tag doesnt work domain.com/tag/tag-tag2.html
 from: loop-tag.php

Doesn't work because /tag/tag-tag2.html doesn't resolve any posts, and
therefor, the 404 handler/templates are used.

Don't use new WP_Query like that, use pre_get_posts or parse_request and
filter the values there instead, that will allow your mail WP_Query to
contain the correct data and not load the 404 handler unless the initial
query doesn't match any posts.

On 17 May 2012 02:45, Chris McCoy <chris at lod.com> wrote:
> // multiple keywords works fine 
> domain.com/search/keyword-keyword2.html
>
> from : loop-search.php
>
> $search_query = str_replace('-',' ', $wp_query->query_vars['s']); 
> $mysearch = new WP_Query("s=$search_query&showposts=-1");
>
> // multiple tag doesnt work domain.com/tag/tag-tag2.html
>
> from: loop-tag.php
>
> $tag_query = str_replace('-',',',$wp_query->query_vars['tag']);
> $mytag = new WP_Query("tag=$tag_query&showposts=-1");
>
> // rewrites
>
> add_action('generate_rewrite_rules', 'hotlinkers_add_rewrite_rules');
>
> function hotlinkers_add_rewrite_rules( $wp_rewrite ) {
>    $new_rules = array(
>    "^search/(.+)\.html$" => 'index.php?s=' . 
> $wp_rewrite->preg_index(1),
>    "^tag/(.+)\.html$" => 'index.php?tag=' . $wp_rewrite->preg_index(1)
>    );
>    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; }
>
> add_filter('query_vars', 'hotlinkers_queryvars' );
>
> function hotlinkers_queryvars( $qvars ){
>    $qvars[] = 's';
>    $qvars[] = 'tag';
>    return $qvars;
> }
>
> add_action('init','hotlinkers_flush_rules');
>
> function hotlinkers_flush_rules(){
>    global $wp_rewrite;
>    $wp_rewrite->flush_rules();
> }
>
> Thanks ;)
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list