WordPress 插件 Search Exclude 可以让我们设置某些文章/页面在搜索结果中排除掉,如果你不想每次发布文章/页面时都要去勾选“Exclude from Search Results(从搜索结果中排除)”,则建议添加相应的代码以便我们在发布文章/页面时自动选中(勾选)“Exclude from Search Results(从搜索结果中排除)”。具体操作办法如下:
将以下代码添加到当前主题的 functions.php 文件中或添加到自定义代码插件中(PS:不懂?请参考『WordPress 小巧易用的自定义代码插件 YGJ-Costom-Code』)即可。
//文章和页面类型自动选中从搜索结果中排除
add_filter('default_content', 'excludeNewPostByDefault', 10, 2);
function excludeNewPostByDefault($content, $post)
{
if ('post' === $post->post_type || 'page' === $post->post_type) {
do_action('searchexclude_hide_from_search', array($post->ID), true);
}
}
如果只是想要页面自动选择从搜索结果中排除,只需要将第 5 行代码修改为以下代码即可。
if ( 'page' === $post->post_type) {
更多关于这款插件的介绍及使用请参考『WordPress 搜索结果排除文章/页面的插件 Search Exclude』。