【问题标题】:Wordpress Search Function to only search postsWordpress 搜索功能仅搜索帖子
【发布时间】:2011-05-11 01:48:22
【问题描述】:

我想使用 wordpress 搜索功能,但我希望它只搜索我的博客文章并从查询中排除我的静态页面。这个怎么做?我不反对使用插件。

【问题讨论】:

  • 更新 WordPress 时,您的修复将被覆盖。

标签: php wordpress search


【解决方案1】:

答案在这里

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');
?>

【讨论】:

  • 接受的答案(和链接的论坛帖子)概述了糟糕的结果。编辑核心文件从来都不是一个好主意,并且在 foreach 循环中跳过结果可能会在涉及分页时导致混乱的事情。这个钩子是一个更好的解决方案!
  • 这实际上不是一个好的答案,除非您只想在您网站上的每个搜索字段中搜索帖子。因此,管理区域和其他任何地方的任何搜索字段都可能会中断,除非您只想将它​​们全部用于搜索帖子。这在实施此解决方案后可能并不明显,但您可以打赌它会导致问题。
  • 有没有办法让这个功能在只出现在博客页面上的搜索栏中工作?这似乎是为 WP 管理员准备的,但我正在尝试在用户端做一些事情,以便他们可以从任何 /blog/ 页面搜索博客文章。
【解决方案2】:

只需在主题搜索表单中添加&lt;input type="hidden" name="post_type" value="post" /&gt;...问候!

【讨论】:

  • 我现在非常爱你。我到处找这个。
  • 很好的解决方案!谢谢!
  • 这应该是最佳答案
  • 不幸的是,这不可能(或容易)在所有情况下都实现。例如,我的搜索表单是 Elementor 制作的,我无法对其进行编辑。
【解决方案3】:

这个 wp 论坛页面有很多不同的示例,说明如何根据您要编辑网站的位置来执行此操作(我相信 index.php 或 wp-includes/query.php 是您的选择):

http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages

【讨论】:

  • 嗯,这似乎是最好的帖子之一 - 但老实说,它并没有很大的帮助。两种主要方法是 A) 破解核心(你不应该这样做) B) 丢弃模板中不需要的结果(效率低下) 必须是一种更简单的方法来控制搜索正在搜索的内容 - 令人惊讶的是 WP 没有为它发挥作用。
  • 如果您查看日期,您会发现帖子已超过 3.5 年。我希望现在有更好的东西与最新版本的 WP 相关。
【解决方案4】:

概述的解决方案很差。编辑核心会阻碍 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 将确保只返回帖子。

【讨论】:

  • 其实这样不行。它返回所有页面以及搜索中的帖子。
  • 实际上,这确实有效。这是 IMO 的最佳解决方案。
【解决方案5】:

如果您没有指定不同的帖子类型,此解决方案会使搜索只检索帖子。如果您在不同搜索字段的隐藏字段中指定自定义帖子类型,此方法不会干扰。

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');

【讨论】:

    【解决方案6】:
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
    <?php if (is_search() && ($post->post_type=='page')) continue; ?>
    

    试试这个,告诉我是否可行。

    【讨论】:

      【解决方案7】:

      将查询与全局查询合并。

         global $wp_query;    
         $args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );  
         query_posts( $args );
      

      【讨论】:

        【解决方案8】:

        您可以使用 WP Search http://wpsear.ch/ 并且可以配置要在结果中显示的帖子类型。

        【讨论】:

        • 该链接现已断开。请删除此答案。
        【解决方案9】:
         '<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>; 
        

        【讨论】:

          【解决方案10】:

          如果使用多个搜索表单,以下代码可用于将特定表单限制为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/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-02-04
            • 1970-01-01
            • 2011-12-19
            • 1970-01-01
            • 1970-01-01
            • 2012-12-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多