From few months I am digging into wordpress for multiple category search options, and finally came up with this.
I just found that wordpress allows multiple tags, which means that if in your URL you type this
+tag2
then it will return all your posts with having tags, tag1 AND tag2
but if you type
,tag2
then it will return all the posts having tag1 OR tag2
Same thing now works for categories also
+cat2
and ,cat2
But this only works for category and tags, I am trying it for search option.
Till now I coded where this URL is working(which means it will search HTML only in the category id 114),
/?s=html&cat=114
In this URL, the comma separator works very well, but not the +
I mean to say
/?s=html&cat=114,115
works very well but
/?s=html&cat=114+115
doesn't works.
Can anyone help me in this? I really want this to work
Still awaiting
From few months I am digging into wordpress for multiple category search options, and finally came up with this.
I just found that wordpress allows multiple tags, which means that if in your URL you type this
http://yourblog/tag/tag1+tag2
then it will return all your posts with having tags, tag1 AND tag2
but if you type
http://yourblog/tag/tag1,tag2
then it will return all the posts having tag1 OR tag2
Same thing now works for categories also
http://yourblog/category/cat1+cat2
and http://yourblog/category/cat1,cat2
But this only works for category and tags, I am trying it for search option.
Till now I coded where this URL is working(which means it will search HTML only in the category id 114),
http://yourblog/?s=html&cat=114
In this URL, the comma separator works very well, but not the +
I mean to say
http://yourblog/?s=html&cat=114,115
works very well but
http://yourblog/?s=html&cat=114+115
doesn't works.
Can anyone help me in this? I really want this to work
Still awaiting
Share Improve this question edited Aug 30, 2011 at 19:26 Niraj Chauhan asked Aug 29, 2011 at 3:58 Niraj ChauhanNiraj Chauhan 8503 gold badges19 silver badges43 bronze badges 1- Can anyone help me in this?????????? – Niraj Chauhan Commented Aug 30, 2011 at 4:41
3 Answers
Reset to default 6 +50The problem is that in a url the '+' sign is equivalent to a space so that's how PHP sees it.
If you use an action on parse_request
you can make this work like so:
add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {
if ( ! isset( $query->query_vars[ 'cat' ] ) )
return $query;
// split cat query on a space to get IDs separated by '+' in URL
$cats = explode( ' ', $query->query_vars[ 'cat' ] );
if ( count( $cats ) > 1 ) {
unset( $query->query_vars[ 'cat' ] );
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}
The above will get category ids passed in with '+' in between them by splitting them on a space which is what PHP sees in the $_GET parameter.
If we have more than one item in the array it must be an 'and' style search so we can pass the array from splitting on the space into the 'category__and'
query var which returns posts in all the specified categories.
Try this: http://yourblog/?s=html&cat[]=114&cat[]=115
BTW, why don't you use a hook link pre_get_posts
to filter your search? It would be much easier than tweaking the search URL.
The solution suggested by @sanchothefat is OK, but leads to problems when pagination is used. For example, you have url like domain/?s=&cat=1+2. When you click on page 2, the URL become domain/?s=&cat=1+2 but redirect_canonical() defined in wp-includes/canonical.php tries to modify url to domain/?s&cat=12 which is not the same... I found easier to me to rewrite category_search_logic() rather investigate redirect_canonical() logic and apply additional filters to it.
This is my way to buypass the canonical logic:
function category_search_logic( $query ) {
if ( ! isset( $_GET['categories'] ) )
return $query;
// split cat query on a space to get IDs separated by ',' in URL
$cats = explode(',', $_GET['categories'] );
if ( count( $cats ) > 1 ) {
unset($_GET['categories']);
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745329624a4622816.html
评论列表(0条)