• 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

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

    Thread Starter maorb

    (@maorb)

    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?

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

    Checkboxes or a radio group..

    Thread Starter maorb

    (@maorb)

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

    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?

    Thread Starter maorb

    (@maorb)

    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?

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

    Or is that the question?… πŸ™‚

    Thread Starter maorb

    (@maorb)

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

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

    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?

    Thread Starter maorb

    (@maorb)

    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…

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

    Thread Starter maorb

    (@maorb)

    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?

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

    Thread Starter maorb

    (@maorb)

    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

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How to filter search via several categories?’ is closed to new replies.