WordPress Search Function to only search posts

I want to use the wordpress search function but I want it to only search my blog posts and exclude my static pages from the query. How to do this? I am not opposed to using a plugin.

Related posts

Leave a Reply

10 comments

  1. This solution makes the search retrieve only posts if you haven’t specified a different post type. This method wont interfere if you specify a custom post type in a hidden field in a different search field.

    function searchFilter($query) {
        if ($query->is_search) {
            if ( !isset($query->query_vars['post_type']) ) {
                $query->set('post_type', 'post');
            }
        }
        return $query;
    }
    add_filter('pre_get_posts','searchFilter');
    
  2. The outlined solutions are poor. Editing the core impedes on upgradeability of the WordPress install – you’ll have to repeat that change every time you update, which you should be doing. The other solution places unnecessary load on the database by filtering results after retrieving them. The better solution:

    In your theme’s functions.php, add a new function to write out a search form:

    function custom_search_form( $form, $value = "Search", $post_type = 'post' ) {
        $form_value = (isset($value)) ? $value : attribute_escape(apply_filters('the_search_query', get_search_query()));
        $form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
        <div>
            <input type="hidden" name="post_type" value="'.$post_type.'" />
            <input type="text" value="' . $form_value . '" name="s" id="s" />
            <input type="submit" id="searchsubmit" value="'.attribute_escape(__('Search')).'" />
        </div>
        </form>';
        return $form;
    }
    

    Now, in the template where you want the form (or within any widgets you’ve created, this could easily be registered as a widget instead), this:

    <?= custom_search_form( null, 'Search posts', 'post'); ?>
    

    The arguments could be left out from the function & call, but I find them helpful. The key to this whole thing is the hidden input ‘post_type’, which passes the value to the query. The default, post, will ensure only posts are returned.

  3. <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
    <?php if (is_search() && ($post->post_type=='page')) continue; ?>
    

    Try this one and tell me if works.

  4.  '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
                    <div>
                        <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
                        <input type="text" value="' . get_search_query() . '" name="s" id="s" />
                        <input type="submit" id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
                    </div>'
                </form>; 
    
  5. If using multiple search forms, the following code is useful to limit a specific form to post_type post.

    /**
     * Modify search query
     *
     * @param WP_Query $query
     *
     * @return void
     */
    function theme_modify_search( $query ) {
     
        if( is_admin() ) {
            return;
        }
            
        if( $query->is_main_query() ) {
            if( isset( $_GET, $_GET['post_type'] ) ) {
                $query->set( 'post_type', $_GET['post_type'] );
            }
        }
     
    }
    add_action( 'pre_get_posts', 'theme_modify_search' );
    

    For more details: https://wordpress.org/support/topic/limit-a-specific-search-form-to-post-only/