ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

"Category search"?

December 03, 2009 4:27am

Subscribe [2]
  • #1 / Dec 03, 2009 4:27am

    Ville Walveranta

    72 posts

    How do I implement a search:simple_form -like search that is limited to specific categories? In other words, I don’t want to display a category selector like in search:advanced_form, but a single search field that is limited to specific categories. The category=“x|y|z” -parameter doesn’t work with search:simple_form, so there must be some other way, I hope. 😊

    Thanks for any advice!

    Ville

  • #2 / Dec 03, 2009 10:37pm

    Ville Walveranta

    72 posts

    Not as easy as I thought?

  • #3 / Dec 03, 2009 11:35pm

    AnotherMuse

    106 posts

    There is nothing that says you have to provide all the options on the advanced search form to your users. So you could build one like:

    {exp:search:advanced_form result_page="search/results" category="1|2|3"}
    <label for="keywords">Keyword:</label>
    <input type="text" name="keywords" /></p>
    
    <input type="submit" value="Search" /></p>
    {/exp:search:advanced_form}

    From there, you can add in whatever other options you want to give the users. It’s been a while since I did this, so you may need to add in some of the other search options as hidden fields if you want to make sure the results page comes out a certain way or only certain options are considered. For example:

    <input type="hidden" name="orderby" value="title" />
    <input type="hidden" name="sort_order" value="asc" />
  • #4 / Dec 06, 2009 1:15am

    Ville Walveranta

    72 posts

    Thanks for that info… I was able to get it working extending that idea some. The “category” variable in the exp:search:advanced_form tag is meaningless (in this regard) because it only defines which categories are displayed on the advanced form for the user to select from. Since I’m only displaying a keyword field they’re not needed. To limit the search to specific categories I used the following:

    {exp:search:advanced_form result_page="search/results"}
    <?php
    foreach ($cat_id_array as $cat_id)
       print "<input type='hidden' name='cat_id[]' value='" . $cat_id . "'>";
    ?>
    <input type="text" name="keywords" id="keywords" value="" size="18" maxlength="100" />
    <input type="submit" id="search-submit" value="Go!" />
    {/exp:search:advanced_form}

    ..where the $cat_id_array is a manually maintained (they change rarely) global array that contains the categories I want to limit this search to:

    global $IN;
    $cat_id_array = explode(",", $IN->global_vars["cat_ids"]);

    I set this in path.php so that the info can be easily accessed on various pages:

    $global_vars = array(
      "cat_ids" => "12,13,15,16,20"
    );
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases