【发布时间】:2017-02-11 16:53:31
【问题描述】:
我有一种自定义帖子类型,可用于使用 uncode 主题构建的页面上的某些文本块。我需要公开这些块,以便它们显示在页面上,但我想阻止它们出现在搜索结果中。
search.php 不像普通的 wordpress 搜索文件,它是 uncode-theme 文件并且没有正常的查询,我不认为所以我想我可能需要一个函数?
谁能告诉我如何做到这一点?
CPT 是“静态内容”
谢谢!
【问题讨论】:
我有一种自定义帖子类型,可用于使用 uncode 主题构建的页面上的某些文本块。我需要公开这些块,以便它们显示在页面上,但我想阻止它们出现在搜索结果中。
search.php 不像普通的 wordpress 搜索文件,它是 uncode-theme 文件并且没有正常的查询,我不认为所以我想我可能需要一个函数?
谁能告诉我如何做到这一点?
CPT 是“静态内容”
谢谢!
【问题讨论】:
这里的答案取决于您是通过自己的代码创建 CPT,还是其他插件正在创建 CPT。有关这两种方法的详细说明,请参阅此链接:
http://www.webtipblog.com/exclude-custom-post-type-search-wordpress/
基本要点是这样的:
如果您要创建自己的 CPT,您可以在 'exclude_from_search' => true 的 register_post_type() 调用中添加一个参数
如果另一个插件/主题正在创建 CPT,您需要稍后设置此 exclude_from_search 变量,作为 CPT 过滤器的一部分,如下所示:
// functions.php
add_action( 'init', 'update_my_custom_type', 99 );
function update_my_custom_type() {
global $wp_post_types;
if ( post_type_exists( 'staticcontent' ) ) {
// exclude from search results
$wp_post_types['staticcontent']->exclude_from_search = true;
}
}
【讨论】:
我认为接受的答案是正确的。 exclude_from_search 阻止所有 $query = new WP_Query 返回结果。
核心说:
...检索任何类型 except 修订和类型 'exclude_from_search' 设置为 TRUE)
这是一个常见的问题,并且与 前端搜索结果页面 混淆。在数据库中搜索帖子。
在前端使用自定义查询呈现内容,需要exclude_from_search = false 或者使用其他方式,直接通过id获取内容。
您需要改为过滤搜索前端机制。这是 true 从搜索中排除帖子类型,无需手动重新构建“已知”类型:
function entex_fn_remove_post_type_from_search_results($query){
/* check is front end main loop content */
if(is_admin() || !$query->is_main_query()) return;
/* check is search result query */
if($query->is_search()){
$post_type_to_remove = 'staticcontent';
/* get all searchable post types */
$searchable_post_types = get_post_types(array('exclude_from_search' => false));
/* make sure you got the proper results, and that your post type is in the results */
if(is_array($searchable_post_types) && in_array($post_type_to_remove, $searchable_post_types)){
/* remove the post type from the array */
unset( $searchable_post_types[ $post_type_to_remove ] );
/* set the query to the remaining searchable post types */
$query->set('post_type', $searchable_post_types);
}
}
}
add_action('pre_get_posts', 'entex_fn_remove_post_type_from_search_results');
并且备注$post_type_to_remove = 'staticcontent'; 可以更改以适应任何其他帖子类型。
请发表评论如果我在这里遗漏了什么,我无法找到另一种方法来防止这样的帖子类型场景,通过查询显示内容但隐藏搜索/直接访问前端用户。
【讨论】:
'exclude_from_search' => true(按照接受的答案的建议)还将从分类档案中删除帖子,这可能不是您想要的。我自己刚刚经历过这个(WP 5.3.2),你可以看到 OP 在接受的答案的 cmets 中也经历过。如果您只想影响搜索结果页面,请使用此答案中的方法。
首先,Jonas Lundman 的答案是正确的,应该是公认的答案。
exclude_from_search 参数工作不正确 - 它也将帖子类型排除在其他查询之外。
WP 问题跟踪系统上有一张票,但他们已将其作为 wontfix 关闭,因为他们无法在不破坏向后兼容性的情况下修复此问题。有关详细信息,请参阅 this ticket 和 this one。
我在 Jonas Lundman 提出的解决方案中添加了额外的检查,因为:
post_type 可能会导致意外结果。add_action('pre_get_posts', 'remove_my_cpt_from_search_results');
function remove_my_cpt_from_search_results($query) {
if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
return $query;
}
// can exclude multiple post types, for ex. array('staticcontent', 'cpt2', 'cpt3')
$post_types_to_exclude = array('staticcontent');
if ($query->get('post_type')) {
$query_post_types = $query->get('post_type');
if (is_string($query_post_types)) {
$query_post_types = explode(',', $query_post_types);
}
} else {
$query_post_types = get_post_types(array('exclude_from_search' => false));
}
if (sizeof(array_intersect($query_post_types, $post_types_to_exclude))) {
$query->set('post_type', array_diff($query_post_types, $post_types_to_exclude));
}
return $query;
}
【讨论】: