WordPress.org

Ready to get started?Download WordPress

Forums

Dropdown sidebar filter by category? (12 posts)

  1. Arash
    Member
    Posted 4 years ago #

    I am importing about 800 post from an only cms to wordpress and these post do not have a category or tag.

    Manually adding tags and categories is a hell of a job! So I was wondering.

    is it possible to make a search box containing 3 different preselected drop-down menu's that users can choose from. The search would then filter based on these preselected words. This will kinda function like a category.

    This is what I have so far

    <form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
      <div>
        <label class="screen-reader-text" for="s">Search for:</label>
    Type
    <select name='s' id='s' class='postform' >
      <option value='0' selected='selected'>All</option>
      <option class="level-0" value="dog">dog</option>
      <option class="level-0" value="cat">cat</option>
    </select>	
    
    Colour
    <select name='+' id='+' class='postform' >
      <option value='0' selected='selected'>All</option>
      <option class="level-0" value="white">white</option>
      <option class="level-0" value="black">black</option>
    </select>	
    
       <input type="submit" id="searchsubmit" value="Search" />
      </div>
    </form>

    But rather than searching for dog AND white. It searches for dog OR white.

  2. David Gard
    Member
    Posted 4 years ago #

    You could possibly use query_posts() - http://codex.wordpress.org/Function_Reference/query_posts, but I'd use a custom query.

    I'm assuming you want the results to be displayed on your index page and that you want only the Post Title to be searched? If so, add this to the top of your index.php page -

    global $wpdb;
    $posts = $wpdb->get_results($wpdb->prepare(
    	'SELECT * FROM '.$wpdb->posts.'
    	WHERE post_title="$_GET['s']"
    	OR post_title="$_GET['+']"
    '));
  3. Arash
    Member
    Posted 4 years ago #

    Thank you, but how can I implement that in the dropdown box?

    The dropdown I made give the following

    ?&s=dog&s=white

    instead of

    ?&s=dog+white

  4. David Gard
    Member
    Posted 4 years ago #

    It's probably because you are using the _GET method rather than the _POST method - I'd advise changing that and then amending the code I gave to this -

    global $wpdb;
    $posts = $wpdb->get_results($wpdb->prepare(
    	'SELECT * FROM '.$wpdb->posts.'
    	WHERE post_title="$_POST['s']"
    	OR post_title="$_POST['+']"
    '));

    To be honest, I'd also recommend changing the name field of your <select> boxes, as 's' and '+' don't really mean much. You can literally name them what you want.

  5. Arash
    Member
    Posted 4 years ago #

    It gives a unexpected T_STRING error. So I changed it into

    global $wpdb;
    $posts = $wpdb->get_results($wpdb->prepare(
    	'SELECT * FROM '.$wpdb->posts.'
    	WHERE post_title="$_POST["s"]"
    	OR post_title="$_POST["+"]"
    '));
  6. David Gard
    Member
    Posted 4 years ago #

    Sorry, that's me typing badly - should be this...

    global $wpdb;
    $posts = $wpdb->get_results($wpdb->prepare(
    	'SELECT * FROM '.$wpdb->posts.'
    	WHERE post_title="'.$_POST['s'].'"
    	OR post_title="'.$_POST['+'].'"
    '));
  7. Arash
    Member
    Posted 4 years ago #

    Thank you.

    The search results are even less accurate and it is still

    ?&s=dog&s=white

    instead of

    ?&s=dog+white

  8. David Gard
    Member
    Posted 4 years ago #

    Ok, I see what you are looking to happen, but I've gone in a slightly different direction. The _POST method is much more scalable than the _GET method, so I'd abandon that. Change your original code that you posted to -

    <form method="post" id="searchform" action="<?php bloginfo('siteurl'); ?>">
      <div>
        <label class="screen-reader-text" for="s">Search for:</label>
    Type
    <select name='field1' id='field1' class='postform' >
      <option value='0' selected='selected'>All</option>
      <option class="level-0" value="dog">dog</option>
      <option class="level-0" value="cat">cat</option>
    </select>	
    
    Colour
    <select name='field2' id='field2' class='postform' >
      <option value='0' selected='selected'>All</option>
      <option class="level-0" value="white">white</option>
      <option class="level-0" value="black">black</option>
    </select>	
    
       <input type="submit" id="searchsubmit" value="Search" />
      </div>
    </form>

    Then, as a starter, add this to the top of your index page (to make sure the correct results are getting through -

    echo '<pre>';
    print_r($_POST);
    echo '</pre>';

    If you have the correct options that you selected for your search in the _POST array, then replace the above code with the code I supplied earlier -

    global $wpdb;
    $posts = $wpdb->get_results($wpdb->prepare(
    	'SELECT * FROM '.$wpdb->posts.'
    	WHERE post_title="'.$_POST['field1'].'"
    	OR post_title="'.$_POST['field2'].'"
    '));
  9. Arash
    Member
    Posted 4 years ago #

    Thank you I am going to try that and let you know the outcome.

  10. Arash
    Member
    Posted 4 years ago #

    The above code does not work at all. :(

    I think there is miscommunications. WordPress has it's own search function and there is an option to search more than one word.

    So all I want to do is have 2 rows of preselected search terms.

    http://www.site.com/?&s=row1+row2

  11. Arash
    Member
    Posted 4 years ago #

    I think something like str_replace should be able to fix it. I am doing some experiments with that.

  12. Arash
    Member
    Posted 4 years ago #

    Anyone have a suggestion? What seems to be a very basic things turns out to be very complicated. I cannot find anyone who has had a similar issue and string replacement does not seem to work with 2 search terms.

Topic Closed

This topic has been closed to new replies.

About this Topic