How to change search value using a drop down selector?

As on the title I’m loooking to make a search form in WordPress using a select dropdown to search the normal WordPress posts and also a custom post type.

The normal posts has a different look and I have a custom post type which has vertical thumbnails

Read More

I figuret the search results out using this function

function template_chooser($template)
{
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'gallery' )
  {
    return locate_template('taxonomy-gallery.php');
  }
  return $template;
}
add_filter('template_include', 'template_chooser');

But my problem is in the search form

The default search form to search default WordPress posts is this one:

<div class="searchbox">
<form action="<?php echo home_url( '/' ); ?>" method="get"><button name="search" class="button">
<span></span></button>
<input type="text" id="s" name="s" value="" />
</form>
</div>

By adding <input type="hidden" name="post_type" value="gallery" /> I can get search results of gallery via taxonomy-gallery.php

So my last search form code is this one

<div class="searchbox">
<form action="<?php echo home_url( '/' ); ?>" method="get"><button name="search" class="button">
<span></span></button>
<input type="text" id="s" name="s" value="" />

<input type="hidden" name="post_type" value="gallery" />


</form>
</div>

But I want to add a select in search input that makes me to choose what I want to search posts or custom post type (gallery)

For example you can check this image

enter image description here

Related posts

1 comment

  1. Selection dropdowns can be made this way:

    <select name="post_type" >
        <option value="post">Posts</option>
        <option value="gallery">Gallery</option>                 
    </select>
    

Comments are closed.