• Hi Mikko,

    Thanks for this great plugin. I would like my users to be able to limit their search to a particular category. Is this possible please?

    Thanks,
    Grant

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yes, the same way it’s done with any WP search: using the category parameters WordPress provides. The easiest way is the Relevanssi searchform shortcode:

    [searchform dropdown="category"]

    This creates a search form with a category dropdown menu.

    Thread Starter Grant McN

    (@grant-mcn)

    Thank you. I am using this code:

    add_filter( 'generate_navigation_search_output', function() {
    echo '<div class="navigation-search">';
    echo do_shortcode( '[searchform dropdown="category"]');
    echo '</div>';
    } );

    But it produces two search bars: https://ibb.co/0sMP3N7 I would like the following, if possible:
    1. The Relevanssi searchbar
    2. To be able to include a custom search message
    3. To only show categories (not subcategories) in the dropdown.

    Please advise if you can help with all/ any of these.
    Thanks.

    Plugin Author Mikko Saari

    (@msaari)

    Instead of using the shortcode, you should probably modify the search bar that is already in your theme. You can see how the category dropdown is composed: it’s a <select> element with an <option> element for each category. If you add the dropdown manually to your existing search form, you can also easily choose which categories are included in it and you can exclude the subcategories.

    I can’t tell you how to modify your existing search bar. If you have a premium theme, ask the theme support.

    Thread Starter Grant McN

    (@grant-mcn)

    Thanks, Mikko. This is the modified search bar code the plugin support has offerd:

    add_filter( 'generate_navigation_search_output', function() {
        echo '<div class="navigation-search">';
        echo do_shortcode( '[searchform dropdown="category"]');
        echo '</div>';
    } );

    compared to the original.

    It’s unclear to me how manually to add in the dropdown using the <select> element with an <option> element for each category. Could you please tell me how and where to add it to the above code if it’s easily done?


    Plugin Author Mikko Saari

    (@msaari)

    For starters, the correct code is this:

    add_filter( 'generate_navigation_search_output', function( $form ) {
    $form = '<div class="navigation-search">';
    $form .= do_shortcode( '[searchform dropdown="category"]');
    $form .= '</div>';
    return $form;
    } );

    This way, the function will override the original search form instead of creating a second search form.

    If you want to modify the search form, you can do this instead:

    add_filter( 'generate_navigation_search_output', function( $form ) {
    $form = '<div class="navigation-search">';
    $form .= <<<END
    <form action="POST"...
    the rest of the form HTML code here
    </form>
    END;
    $form .= '</div>';
    return $form;
    } );

    You can take the Relevanssi search form HTML code from the page and paste it in the function. Then you can easily change the parts you want to change: remove categories, change texts and so on.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.