I want to add <input type="hidden" name="post_type" value="product"> to search form for any theme i would use.

I read that i should not modify theme itself but create a plugin.

So i created the plugin:

add_filter('get_search_form', 'my_search_form');
function my_search_form($html) {
 $html ='

 <form role="search" method="get" class="searchform" action="'.esc_url( home_url( '/' ) ).'>
<input type="hidden" name="post_type" value="product">
    <input type="search" class="search-field" placeholder="'.esc_attr_x( 'Search &hellip;', 'placeholder', 'estore' ).'" value="'.esc_attr( get_search_query() ).'" name="s">
    <button type="submit" class="searchsubmit" name="submit" value="'.esc_attr_x( 'Search', 'submit button', 'estore' ).'"><i class="fa fa-search"></i></button>

 return $html;

The theme has searchform.php with original form and the plugin does not change it.

What should i do?

  • So you only want to make the product post type searchable on the front-end and not post and page ?
    – birgire
    Jun 6 '17 at 14:14
  • @birgire yes, the only difference is that i want to make it via plugin and not hard-code it.
    – Mike
    Jun 6 '17 at 14:59

Here's an alternative way to only make the product post type searchable, from a plugin code:

/** Plugin Name: Product Search Only **/

add_filter( 'register_post_type_args', function( $args, $post_type )
    $args['exclude_from_search'] = true;

    if( 'product' === $post_type && (bool) $args['public'] )
        $args['exclude_from_search'] = false;

    return $args;
}, 999, 2 );

Here we modify the post type registration, by setting the exclude_from_search to true, for all post types, except the public product post type. If needed you can adjust the late priority.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.