【发布时间】:2011-05-11 01:48:22
【问题描述】:
我想使用 wordpress 搜索功能,但我希望它只搜索我的博客文章并从查询中排除我的静态页面。这个怎么做?我不反对使用插件。
【问题讨论】:
-
更新 WordPress 时,您的修复将被覆盖。
我想使用 wordpress 搜索功能,但我希望它只搜索我的博客文章并从查询中排除我的静态页面。这个怎么做?我不反对使用插件。
【问题讨论】:
答案在这里
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');
?>
【讨论】:
只需在主题搜索表单中添加<input type="hidden" name="post_type" value="post" />...问候!
【讨论】:
这个 wp 论坛页面有很多不同的示例,说明如何根据您要编辑网站的位置来执行此操作(我相信 index.php 或 wp-includes/query.php 是您的选择):
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
【讨论】:
概述的解决方案很差。编辑核心会阻碍 WordPress 安装的可升级性 - 每次更新时都必须重复该更改,这是您应该做的。另一种解决方案通过在检索结果后过滤结果 对数据库施加不必要的负载。更好的解决方案:
在你的主题的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;
}
现在,在您想要表单的模板中(或在您创建的任何小部件中,可以轻松地将其注册为小部件),这样:
<?= custom_search_form( null, 'Search posts', 'post'); ?>
参数可以从函数 & 调用中省略,但我发现它们很有帮助。整个事情的关键是隐藏的输入“post_type”,它将值传递给查询。默认值 post 将确保只返回帖子。
【讨论】:
如果您没有指定不同的帖子类型,此解决方案会使搜索只检索帖子。如果您在不同搜索字段的隐藏字段中指定自定义帖子类型,此方法不会干扰。
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');
【讨论】:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
试试这个,告诉我是否可行。
【讨论】:
将查询与全局查询合并。
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
【讨论】:
您可以使用 WP Search http://wpsear.ch/ 并且可以配置要在结果中显示的帖子类型。
【讨论】:
'<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>;
【讨论】:
如果使用多个搜索表单,以下代码可用于将特定表单限制为post_type 帖子。
/**
* 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' );
更多详情:https://wordpress.org/support/topic/limit-a-specific-search-form-to-post-only/
【讨论】: