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

Dion Hulse (dd32) wordpress at dd32.id.au
Thu May 17 02:07:01 UTC 2012


The 2 tags may be in use, by no posts have the tag 'tag-tag2', which
is what your request is actually asking for.

You need to understand how WordPress parses requests in order to
understand what's going on:

 1. WordPress is loaded
 2. The rewrite rules are applied
 3. The results form the rewrite rules (the query vars) are loaded
 4. The Query is executed using those rewrite rules
    4a. If no posts are found for the query, 404 template is selected,
continue to 6
    4b. If posts are found, continue to 5
 5. The Request type is determined for the template to use,
is_archive() is_taxonomy(), is_single(), etc.
 6. The Template is loaded and the loop starts.

Your code is running after this so:
 7. The Template is executed, and a new WP_Query is made with the alterations.

Since 7 is within a template, 4b must be chosen, so there has to be
posts for the ORIGINAL query vars, WordPress doesn't know about your
custom alterations until it loads the template, and it may never load
the template if it didn't find posts to start with.

The best way, as suggested, is to use pre_get_posts to hook in between
steps 3 and 4, you can edit the request there, you'll need to limit
the WP_Query instances you modify though, so for that, have a look at
the details in the following presentation by Nacin:

http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-netherlands-2012
specifically, slides 49 onwards (but please, read it all, or watch the
presentation if you can find it online - I didn't look hard and didn't
see it)

On 17 May 2012 11:58, Chris McCoy <chris at lod.com> wrote:
> 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
>
> _______________________________________________
> 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