WordPress.org

Ready to get started?Download WordPress

Forums

Multi-Category Search Form (8 posts)

  1. MrBeta
    Member
    Posted 3 years ago #

    I am trying to code up a multi-category search form. I know there is a plugin for this, but it does not produce the output I am looking for. Basically, I want to be able to filter posts by one or more categories. So far, (I haven't written the code yet) this is my idea, I just want to know if anyone thinks this will work.

    A search form that will include drop down menus each listing categories. For example, the first dropdown menu would be vehicle make and the second would be body style.

    So the input would be something like <input type="select" name="make"> for example, and upon submitting the form, would initiate the action where $variable1 = "make"; and $variable2 = "bodystyle";

    then a wp function like possibly get_posts('category'=>$variable1&$variable2);

    I am not sure if you can use variables inside of a wp function like that or not. Any ideas on the best way to implement this would be most appreciated.

    Thanks

  2. nemoprincess
    Member
    Posted 2 years ago #

    Hello MrBeta, I' m trying to do something similar, but with checkbox. Any update in your work?

    Thanks

  3. MrBeta
    Member
    Posted 2 years ago #

    Hey nemoprincess, I ended up using the WP Custom Fields Search in this particular instance, however, it does not have the option of using a checkbox. It only uses radio buttons, drop down, and text.

    What exactly are you trying to do and I will see if I can help.

  4. nemoprincess
    Member
    Posted 2 years ago #

    Hi MrBeta, thanks for your answer. This is the code included in my widget:

    <form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
            <label class="screen-reader-text" for="s">Cerca</label>
    		<input type="text" value="" name="s" id="s" /></br>
    
    <?php
    $categories = get_categories();
    $num_cat_sel = 0;
    foreach ($categories as $cat) {
        	$cat_name = $cat->cat_name;
        	$catid = get_cat_ID($cat_name);
       		$termorder = $cat->term_order;
    		echo "<input type='checkbox' name='cat' value='".get_cat_ID($cat_name)."'>";
        	echo $cat->cat_name;
    		$num_cat_sel++;
    
        } 
    
    ?>		
    
    		</br><input type="submit" id="searchsubmit" value="Cerca" />
        </form>

    It prints out a search in this way:

    s=mysearchedstring&cat=id_first_cat&cat=id_second_cat

    My problem is the it should be in this way:

    s=mysearchedstring&cat=id_first_cat,id_second_cat

    Any idea? Thanks a lot!

  5. MrBeta
    Member
    Posted 2 years ago #

    Can you elaborate a little bit more on what you want to accomplish? I see that you have a checkbox populated with category names, what is the overall end result that you are wanting?

    Sorry just want to understand exactly.

  6. nemoprincess
    Member
    Posted 2 years ago #

    Yes the checkbox is perfect. The problem is how to retrieve posts that are in both categories (in this example we have 2 categories) and not in just one of them.

  7. MrBeta
    Member
    Posted 2 years ago #

    I have come across this plugin, that may do what you are trying to accomplish. Let me know if this helps.

    http://www.zirona.com/software/wordpress-advanced-search

    It is not available on the wordpress.org website.

    If this doesnt work, we can look at some more detailed options. :)

  8. nemoprincess
    Member
    Posted 2 years ago #

    I tried it, but it' s not what I need. Anyway I found a solution.
    I have a widget to display the form above, with some small changes.
    I would like to insert a bit of code, a custom query post in every search.php template file if my plugin is on. Do you know how I can do it?

    Thanks! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic