0

I'm trying to limit the search function on my website to online content with the blog's folder. The reason is because currently, search works across the whole website, which in theory is great except that it retrieves awkwardly formatted photo galleries that span over all other content.

http://andybloxham.com

That's the website in question. I'd like for the search function to only find content from within /words

The search tool is the standard one available in Customize. The theme is Solofolio. I'm guessing the variables I would change are in searchform.php?

<form method="get" class="searchform" action="<?php echo esc_url(home_url()); ?>/">
<input type="text"
     value="<?php _e( 'Search', 'solofolio' ); ?>"
     name="s"
     id="s"
     onblur="if (this.value == '') {this.value = 'Search';}"
     onfocus="if (this.value == 'Search') {this.value = '';}" />
<span><button type="submit" class="sidebar-search fa fa-search"></button></span>
</form>
2

There is not "blog" folder in WordPress and the search is not done in folders, it is done in the database. I assume you mean limit the search to standard post types.

You could add post_type argument to the search form to include only the standard post type:

<form method="get" class="searchform" action="<?php echo esc_url(home_url()); ?>/">
    <input type="text"
         value="<?php _e( 'Search', 'solofolio' ); ?>"
         name="s"
         id="s"
         onblur="if (this.value == '') {this.value = 'Search';}"
         onfocus="if (this.value == 'Search') {this.value = '';}" />
    <inptu type="hidden" value="post" name="post_type" />
    <span><button type="submit" class="sidebar-search fa fa-search"></button></span>
</form>

Or better, hook in pre_get_post:

add_action( 'pre_get_post', function( $query ) {

    if( $query->is_main_query() && !is_admin() && $query->is_search ) {

        //You can include more post types here if you want
        $query->set( 'post_type', array( 'post' ) );

    }

});

Or even better, and the best way to exclude post types from search, just set the exclude_from_search argument to true when registering the post type.

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.