Quantcast
Viewing all articles
Browse latest Browse all 20145

Reply To: [Assigned] Limit search to custom post type

Hi jeffR-3,

I suggest you try wordpress action hook: pre_get_posts
The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary).
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

For example if your hidden field stores your custom post type slug "my-type", like this:

<input type="hidden" name="post_type" value="my-type">

you can try add codes in your theme functions.php:

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() && isset($_REQUEST['post_type'])) {
    if ($query->is_search) {
      $query->set('post_type', array( $_REQUEST['post_type'] ) );
    }
  }
}

add_action('pre_get_posts','search_filter');

Viewing all articles
Browse latest Browse all 20145

Trending Articles