php Wordpress 搜索功能只搜索帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4198842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress Search Function to only search posts
提问by Davey
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.
我想使用 wordpress 搜索功能,但我希望它只搜索我的博客文章并从查询中排除我的静态页面。这该怎么做?我不反对使用插件。
采纳答案by wajiw
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):
这个 wp 论坛页面有很多不同的例子来说明如何执行此操作,具体取决于您要编辑站点的位置(我相信 index.php 或 wp-includes/query.php 是您的选项):
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
回答by THRIVE
Answer is here
答案在这里
http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/
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');
?>
回答by Marcos
Simply add <input type="hidden" name="post_type" value="post" />
to the theme search form... regards!
只需添加 <input type="hidden" name="post_type" value="post" />
到主题搜索表单...问候!
回答by Gavin
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 afterretrieving them. The better solution:
概述的解决方案很差。编辑核心会妨碍 WordPress 安装的可升级性 - 您每次更新时都必须重复该更改,这是您应该做的。另一种解决方案通过在检索结果后过滤结果来对数据库施加不必要的负载。更好的解决方案:
In your theme's functions.php, add a new function to write out a search form:
在您主题的functions.php 中,添加一个新函数来写出一个搜索表单:
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.
参数可以从函数和调用中省略,但我发现它们很有帮助。整个事情的关键是隐藏的输入“post_type”,它将值传递给查询。默认值 ,post
将确保仅返回帖子。
回答by fomicz
<?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.
试试这个,告诉我是否有效。
回答by Gustavo
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');
回答by Gowthaman D
'<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>;
回答by simanchala
Merge the query with global query.
将查询与全局查询合并。
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
回答by Hmayak Tigranyan
You can use WP Search http://wpsear.ch/and you can configure which post types to show in results.
您可以使用 WP Search http://wpsear.ch/,并且可以配置要在结果中显示的帖子类型。