search by specific category ?
-
I searched this but could not find anything. Is there a way to limit the search to only a certain category or categories ? thanks
-
I imagine you’d have to hack the search script to limit the search to one category. I’m confident that it can be done, since it’s pretty easy to do this as an SQL query.
anybody know specifically how to do this ?
bump ? anyone
Again, a search like my http://www.richardc020.com/index.php?cat=6&s=njtransit still lists results not in cat=6. Why and how to fix.
Reply.
I second the request. This is something that I remember being able to do in GreyMatter and since WP is much more robust than GM, I’d expect a similar ability. Any takers?
hey, i know exactly how to do what you guys want.
to limit search results to a specific category you have to include an if/then test inside TheLoop. hopefully everyone will take example from me and learn it’s better to INCLUDE CONTEXTUAL EXAMPLES! here’s mine :
** original code from search.php, using wp1.5.1 **
<?php while (have_posts()) : the_post(); ?><div class=”post”>
<h3 id=”post-<?php the_ID(); ?>”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h3>
<small><?php the_time(‘l, F jS, Y’) ?></small><div class=”entry”>
<?php the_excerpt() ?>
</div><p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’,”,’|‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></div>
<?php endwhile; ?>
** modified code to search ONLY cat #2 **
<?php while (have_posts()) : the_post(); ?><?php if ( in_category(2) ) { ?> <!– check to see if this is in the blog –>
<div class=”post”>
<h3 id=”post-<?php the_ID(); ?>”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h3>
<small><?php the_time(‘l, F jS, Y’) ?></small><div class=”entry”>
<?php the_excerpt() ?>
</div><p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’,”,’|‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></div>
<?php } ?> <!– Close the if statement. –>
<?php endwhile; ?>
**end of code**
as you can see, you simply use the if/then (which i commented) to check and make sure the post being run thru the loop is inside your specified category. copy and paste the code to a text editor to look at it without word-wrap, and it’s really pretty simple.
as with any if/then modifiers, you can have more than one. just make each seperate, you can also stack the qualifiers to return results that are IN one category and NOT in another. using the exclusion syntax as shown on the codex.
good luck!
Here’s a novice php question to follow up on the suggestion above:
I have two categories, and I want to make a search box for each that is limited to just that category.
Is it possible to create custom searchform and search result page pairs? That is, can I create a searchform called “searchform-1.php” and call within that an action of “search-1.php” to display the results of a search within just the first category?
Thanks for any thoughts.
LK – Yes you can do that.
This thread’s issue (search for 2+ conditions one of which is a category limitation doesn’t obey) is still unresolved as of WP 2.0 and I don’t want to have to edit search.php or for that matter *any* .php file every time I reinstall just to get this item fixed. There’s probably some procedure to log it in Trac or however such issues are actually fixed.
The topic ‘search by specific category ?’ is closed to new replies.