Search by category? (16 posts)

  1. Younghusband
    Posted 10 years ago #

    How difficult would it be to whip up an "advanced search" interface that allowed you to do a search within a single category? I envision a drop-down menu that is automagically populated with the categories of the blog, and a form for seach terms.

    To anybody's knowledge, has this been done before?

  2. Younghusband
    Posted 10 years ago #

    So, not a possibility then? What about the WP forums yahoo search with a dropdown for restricting the search to certain categories?

  3. zefefre
    Posted 10 years ago #

    Umm,, I have the same problem, don't know the solution though.
    It would be great if visitors can search within a category.

  4. Dgold
    Posted 10 years ago #

    I dont' know why someone hasn't made this a plugin yet? It could be done in theory, simply because IIRC, you can limit a search to certain categories by putting &cat=1,3,5 at the end of your Search term URI (where the commas are for multiple categories*).

    Because of the ability/option to choose multiple categories, I don't want a drop-down menu, you want radio buttons (checklist style... check all that apply, searching in 1 or more cats).

    I'm interested in this, but also, such a plugin would hopefully also allow my users to search by matching-value in a certain *custom field*. Example, search all posts where custom field key is "Mood" and value is "Happy" would give results from all the posts where I was happy.

    To sum up:
    * Make a plugin with ability to limit Search to 1 or more categories using radio buttons/checklist.
    * Make a plugin with ability to limit Search to posts with a certain value in a custom field.
    * Not me, I don't know the code

  5. aylia
    Posted 10 years ago #

    Anybody? I've been looking for how to set up an advanced search too..

  6. Younghusband
    Posted 10 years ago #

    I'm glad I am not the only one who is looking for this feature. Been two months though, and no bite from any of the advanced coders out there...

  7. Kafkaesqui

    Posted 10 years ago #

    This functionality already exists in WordPress:


    Just place <?php dropdown_cats(); ?> in your search form where you want the category dropdown to display. And by that I mean, after the <form> but before the </form> for your search.

    You may want to provide a label for it, such as "Search in category," but that's up to you.

  8. Younghusband
    Posted 10 years ago #

    Great! Just tested and it works brilliantly! Thanks for the tip Kafkaesqui!

  9. Dgold
    Posted 10 years ago #

    That's cool, Kaf. Good feature. Built in. Didn't know it. I have read that Codex page before but it didn't register for me that it can be used in the Search form.

    I'm still looking for a method to search by Custom Field key/value. Example, search posts (or return a list of posts, and put them on a page) where custom field key=Mood and value=Happy.

    That's a made-up example. In my case I actually want to search for posts where custom field key=AlbumTitle and value=MyFirstCD. It would return a list of songs that are on that album, because every song is a post.

  10. Kafkaesqui

    Posted 10 years ago #

    Dgold, for that I think it's time for another bit of code. No, not just for you. :)

    This request comes up occasionally on the forums, and it would be nice to provide a solution for it. After thinking it through a bit I've built a working if dirty fix to the problem. Now I'm off to see if I can roll it into a plugin.

    Whether the fix is pluggable or not, I'll post again later on with details.

  11. Kafkaesqui

    Posted 10 years ago #

    Turns out it was pluggable.

    Search Custom Fields plugin:

  12. geoffe
    Posted 10 years ago #

    For more help with displaying custom fields for posts, see:

  13. erb
    Posted 10 years ago #

    I tried adding <?php dropdown_cats(); ?> to my search form, but I'm noticing that if "s=" is included, everything else is ignored. I tried a slew of things posted at


    but nothing had any effect regardless of the order or anything of variables in the search query. Using "cat=" is the main thing I want to use.

    I'm still on the hunt, but any suggestions or ideas for what's going wrong would be appreciated (I have the newest version of WordPress, FWIW).

  14. erb
    Posted 10 years ago #

    I think this is my problem/solution


    Although unlike this poster, my regular search didn't work as well as the search reloaded plugin. With his hack it does.

    Partway there; now just need to be able to accept multiple categories.

  15. erb
    Posted 10 years ago #

    Oh my! I confused myself horribly: so regular search does work with multiple categories for me, I just didn't realize it because I had only commented out my call to Search Reloaded, not deactivated it. Once I deactivated it, I could call multiple categories to search.

    My next problem was to allow it to search only posts belong to all of the categories listed, and not just any; fortunately I stumbled across just such a plug in:


    So now I just needed to code an "advanced search" web form that allowed users to enter a search term, select the categories to be searched, and choose whether or not the posts returned appear in any category chosen or all categories chosen.

    The first issue I encountered is that if no search term is entered, the archive template is returned rather than the search template, but it appears the actual results returned on the archive page are accurate, so I think simply modifying the archive page to explain the results more accurately will be okay...

    Another problem I had to get around was serializing the checkbox values (categories each have their own checkbox on my advanced search webform) within the querystring, as opposed to separate key/value pairs (as separate pairs, only the last cat= is utilized by the system); in other words, change from this cat=1&cat=2&cat=3 to this cat=1,2,3. My solution was to process the submission before sending it on to the search page

    $s = $_REQUEST['s'];
    $intersect = $_REQUEST['intersect'];
    $url = $_SERVER['REQUEST_URI'];
    $cats = $_REQUEST[cats];
    $cat = implode(",", $cats);


    I keep thinking there must be a more clever way to get around this, so I’d be curious to hear ideas, but this is what I’ve got for now.

    My next problem is my extensive use of child categories; the first issue resulting from this is that the intersect plugin chokes on parent categories and the second is I don’t always want a parent included when a child is selected for search (I believe this second issue is the reason for the choke--Intersect is being sent all these “extra” categories it wasn’t expecting--so I’m hoping the solution to one is the solution to both).

  16. ebaltz
    Posted 9 years ago #

    I just added <input name="cat" type="hidden" value="**put your cat id here**" />

    To my form. Very simple.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.