Reputation: 1324
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.
Upvotes: 23
Views: 79482
Reputation: 468
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/
Upvotes: 0
Reputation: 579
'<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>;
Upvotes: 0
Reputation: 2478
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');
Upvotes: 6
Reputation: 62
You can use WP Search http://wpsear.ch/ and you can configure which post types to show in results.
Upvotes: 0
Reputation: 127
Merge the query with global query.
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
Upvotes: 0
Reputation: 461
Simply add <input type="hidden" name="post_type" value="post" />
to the theme search form... regards!
Upvotes: 46
Reputation: 5636
Answer is here
http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/
<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
Upvotes: 48
Reputation: 2567
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.
Upvotes: 3
Reputation: 1792
<?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.
Upvotes: 1
Reputation: 12269
This wp forum page has a bunch of different examples of how to do this depending on where you want to edit your site (index.php or wp-includes/query.php are your options I believe):
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
Upvotes: 4