• 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:
    https://www.yoursite.com/?s=searchterm&cat=4,13,22

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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)

    Example:

    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 ??

    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

    Thread Starter mylagoon

    (@mylagoon)

    @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.

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

    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″ />
    </form>

    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.

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

    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" />

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Category Specific Search’ is closed to new replies.