[wp-trac] [WordPress Trac] #16627: Rewrite query vars are stripped from custom taxonomy page URLs via redirect_canonical()

WordPress Trac wp-trac at lists.automattic.com
Thu Mar 10 18:52:58 UTC 2011


#16627: Rewrite query vars are stripped from custom taxonomy page URLs via
redirect_canonical()
---------------------------+--------------------
 Reporter:  gnoodl         |       Owner:
     Type:  defect (bug)   |      Status:  new
 Priority:  normal         |   Milestone:  3.1.1
Component:  Rewrite Rules  |     Version:  3.1
 Severity:  normal         |  Resolution:
 Keywords:                 |
---------------------------+--------------------

Comment (by emartin24):

 Here's what I am using. Worked in 3.0.5, not in 3.1:

 {{{
 // create the custom taxonomies
 function btf_create_taxonomies() {
         register_taxonomy('textures', 'post', array('hierarchical' =>
 false, 'label' => 'Texture Categories', 'query_var' => true, 'rewrite' =>
 true));
         register_taxonomy('wallpapers', 'post', array('hierarchical' =>
 false, 'label' => 'Wallpaper Categories', 'query_var' => true, 'rewrite'
 => true));
 }
 add_action('init', 'btf_create_taxonomies', 0);

 // add the custom query var
 function btf_query_vars($query_vars) {
         $query_vars[] = 'sort';
         return $query_vars;
 }
 add_filter('query_vars', 'btf_query_vars');

 // I had some issues with custom query vars and paging,
 // so I wrote this to correct the query string values
 function btf_request($query_string) {
         if (isset($query_string['name']) && $query_string['name'] ===
 'page'
                         && isset($query_string['page'])) {
                 unset($query_string['name']);
                 $query_string['paged'] = str_replace('/', '',
 $query_string['page']);
         }

         $sort = array('date', 'popularity', 'comments', 'downloads',
 'rating', 'votes');
         if (isset($query_string['name']) &&
 isset($query_string['category_name'])
                         && btf_ends_with('/sort',
 $query_string['category_name'])) {
                 if (strpos($query_string['name'], '-') > 0) {
                         $valid = explode('-', $query_string['name']);
                         $s = $valid[0];
                 }
                 else {
                         $s = $query_string['name'];
                 }

                 if (in_array($s, $sort)) {
                         $parts = explode('/',
 $query_string['category_name']);
                         $query_string['sort'] = $query_string['name'];
                         $query_string['category_name'] = $parts[0];
                         unset($query_string['name']);
                 }
         }
         return $query_string;
 }
 add_filter('request', 'btf_request');
 }}}

 I hope the btf_request function doesn't over-complicate things. Basically,
 what I want is to be able to have:

 - mysite.com/custom-taxonomy/ - default listing
 - mysite.com/custom-taxonomy/sort/downloads - sort by downloads
 - mysite.com/custom-taxonomy/sort/downloads/page/2 - sort by downloads,
 page 2

 The code I have above worked fine in 3.0.5. If there is a better way to
 achieve this, I'm happy to try a different approach.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/16627#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list