Category Specific Search (8 posts)

  1. mylagoon
    Posted 9 years ago #

    I'm looking for a way to edit the search form output and how it interacts with WP.

    I need a search box that will only search specific predetermined categories, let's say cat 4, 13, and 22.

    The url structure below works exactly how I need:

    It's a matter of figuring out how to get WP to add &cat=4,13,22 to the end of a search string that's been entered into the search box.

    Any ideas?

  2. Aleister
    Posted 9 years ago #

    One option is to make a plugin to help with the new functionality you need.

    Any time you want to add new query variables to WordPress, you must declare them so they can be processed. (Or is 'cat' already a valid query variable? I forget.. if it is, you might want to call yours something else)


    function my_query_vars ($vars) {
    $vars[] = "cat";
    return $vars;

    add_filter('query_vars', 'my_query_vars');

    Without that, you will not be able to read the data from $cat.

    Now to read the data from the new variable, the method depends on the use_verbose_rule variable

    if ($wp_rewrite->use_verbose_rules || !isset($wp_rewrite->use_verbose_rules)) {
    the data must be referenced as $1
    } else {
    the data must be referenced as $matches[1]

    That shows you how to get the first variable passed through the query, etc..

    Now you just have to use the data to modify the search results.. which I have no idea about. I have not worked with the base search code ;)

  3. Dgold
    Posted 9 years ago #

    i haven't tried the Search Reloaded plugin but this sounds like it would be a good feature for that

    the ability to have Radio buttons with Advanced Search conditions under your search bar would be really nice. Example,

    SEARCH |_____________|

    Search for:
    Any Of The Terms
    All Of The Terms

    Click which categories:
    Cat 1
    Cat 2
    Cat 3
    Cat 4

    Show posts by date:
    All posts
    Posts from 2005
    Posts from 2004
    Posts from 2003

  4. mylagoon
    Posted 9 years ago #

    @Aleister: I can see the logic behind what you're suggesting, but my PHP skills are limited to me fooling around with WP, and that just doesn't cut it sometimes (like trying to create a plugin).

    Personally I don't think I'd be able to.

    If someone who can write a plugin like this wants to, please, I'd be very grateful, but of course any other proposals to solve this would also be appreciated.

  5. Aleister
    Posted 9 years ago #

    I may see about coming up with something when I make my next plugin. :)

  6. th3rmite
    Posted 9 years ago #

    OK mylagoon basically to add the categories to the end of the search term you need to add a hidden field with the values that you want added to the string. The script to use would be like this:
    <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <input type="text" name="s" id="s" size="20" value="search in blog..." />
    <input type="hidden" name="cat" value="4,13,22" />

    It would be the last input tag that would do it for you
    "<input type="hidden" name="cat" value="4,13,22" />"

    Hope this helps.

  7. thepete
    Posted 9 years ago #

    th3rmite, thanks for your code--that was perfect!! It does exactly what I need it to. SWEET.

  8. cahier
    Posted 9 years ago #

    Great advice, this is exactly what I needed too. After installing the plug-in vCalendar, search wasn't able to find anything anymore...

    However, using comma's to separate the cat id's didn't work for me. Workaround was repeating the hidden input fields:

    <input type="hidden" name="cat" value="13" />
    <input type="hidden" name="cat" value="8" />

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.