WordPress.org

Ready to get started?Download WordPress

Forums

How to filter search via several categories? (18 posts)

  1. maorb
    Member
    Posted 4 years ago #

    Hi ,

    Suppose I want to filter a search by certain posts' categories, I know I might write in the search form a select box like this:

    <select name="cat">
    		<option value="1">Category A</option>
    		<option value="2">Category B</option>
    		<option value="3">Category C</option>
    		// ...and so on
    </select>

    But what if I want that one of the user's selections will perform search via several categories? How that can be passed throug the form?
    Is it possible to pass more than one value in an option?
    I tried something like :

    <option value="3,4,5,6">Search in Articles</option>

    but it didn't worked and searched through all categories, not only these four.
    Is there a way to do that?

    Thanks,
    M

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Add multiple="multiple" to the select tag if you want multiple selections..

    You can't use a single selection for multiple values, or at least i don't think that's an intended usage..

  3. maorb
    Member
    Posted 4 years ago #

    multiple=multiple is not good for me
    It makes the list a list box instead of select box, and the user should choose the items by himself.
    I want the user to choose search articles, and it will search in all articles' categories, since there are more categories, which are not articles...

    Any other suggestions for that?

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    If you want multiple selections then a selection box isn't suited.

    Checkboxes or a radio group..

  5. maorb
    Member
    Posted 4 years ago #

    How can I group some different values on a one selected radio button?

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Give it a value like any other (but something that's never going to be in use, then do the conditionalising with the code that grabs the posts...

    Can i see the code you're using to sort/grab the search results?

  7. maorb
    Member
    Posted 4 years ago #

    This is the form itself (Where I put now 999 in articles, which is a category id that not exist, article are on categories 6-9)

    <form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
      <div>
        <label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />     
    
    	<select id="cat" class="postform" name="cat">
    		<option value="3">News</option>
    		<option value="4">Interviews</option>
    		<option value="5">Companies</option>
    		<option value="999">Articles</option>
    	</select>    
    
        <input  class="btn" type="submit" id="searchsubmit" value="Search" />
    
      </div>
    </form>

    WordPress search grabs the results and a regular loop in a search.php is showing them. Which more code do I have to change?

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    I'm more interested in how you intercept the selection and pass along the values to query posts.

    Or is that the question?... :)

  9. maorb
    Member
    Posted 4 years ago #

    Well, This form as it is passes the data to the search of WordPress or a search plugin, and that function deals with the data to bring the results..
    Thus, yes, the original question, which I still don't have an accurate anser for, was how to pass from the form the data as I want (of several categories in on selction value).

    I think maybe adding a special custom field to each post in all the "articles" categories, and search only the posts that have this custom field. Not the best way, but might be sort of a solution..

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    ..maybe?
    <option value="10,20,99">Several cats</option>
    where 10,20,99 are the cat IDs...

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    Sorry that won't work, the commas get converted..

    Are you using a specific plugin or just letting WordPress do the fetching for search results?

  12. maorb
    Member
    Posted 4 years ago #

    On my first post ogf this thread I wrote that I already tried that with commas, but unfortunatelly with no success..

    I use the Relevanssi plugin for search, thus I'll be able to search a custom field, but the prolem is how to tell the function to deal with multiple values from a form. It seems as you said the the select box doesn't support more than one value per option...

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    You could give the option a value like 88-89-90 (hyphens will work), then replace them when you come to selecting the results (the query), of course if a plugin manages this for you then you're going to need to dig into the plugin code..

  14. maorb
    Member
    Posted 4 years ago #

    Well, if it's not a plugin and just the default wordpress search function, that's the same probem, or even worse since if you deal with the search function it will be overriden on the next WP update..
    Anywaym, I don't know how to do it in the server side.

    Anyone any ideas for that?

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    No it's not the same problem, you can do any number of things in a theme's search.php to handle the sorting of results...

    You pass parameters into query_posts in search.php as you would any other page, the difference being that you need to capture the search parameters.

    $s - contains the search string

    Here's a copy of what i have in my test install's search.php (in my theme folder)...
    http://wordpress.pastebin.com/f3ae7f11f

    Hopefully it will give you a better idea of the possibilities..

    Of course you need to add the additional selection boxes (etc..) to your searchform, i can give you an example one to work with if you like..

    No plugins, no core edits, only requirement is to have a search.php and searchform.php in your theme..

  16. maorb
    Member
    Posted 4 years ago #

    I have a search.php and a searchform.php.
    Currently the searchform.php is sort of what I wrote earlier in this thread, and my current search.php is a regular loop designed to show the results nice with pagination and a search form at its bottom.

    I haven't understood all the code in your example, but I'd be very happy and appreciate that if you might help me with what changes I should do to my search.php in order it will handle multiple values that are being sent through a select option in the searchform.php

  17. Rob
    Member
    Posted 4 years ago #

    I use this script on one of my blogs to handle category filtering. It pulls the category information dynamically so you don't have to add category values to the form every time you create one.

    <form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
      <div>
        <label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        in <?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
        <input type="submit" id="searchsubmit" value="Search" />
      </div>
    </form>

    As far as selecting multiple specific categories it should be easy enough to create a form with radio buttons or check boxes in place of the drop down. I am not entirely sure how this would be done dynamically to generate these as the category list grows but it can definitely be hard coded into the form.

  18. tedzao
    Member
    Posted 3 years ago #

    can this be done with custom fields too?

Topic Closed

This topic has been closed to new replies.

About this Topic