【问题标题】:How to stop display in search results custom post type in WordPress如何停止在 WordPress 中的搜索结果自定义帖子类型中显示
【发布时间】:2018-10-29 13:29:04
【问题描述】:

我有多种自定义帖子类型。我需要显示countrylocation 结果。但目前在search.php 页面中显示所有帖子类型的结果。我需要隐藏这些帖子类型post ,page, experience, trip-theme 的结果。

我正在使用默认主题 WordPress Seventeen theme

我不知道该怎么做。请帮帮我。

谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    当您注册帖子类型时,请为搜索设置一个额外的参数。

    FOR 自定义帖子类型。

    'exclude_from_search' => true,
    

    https://codex.wordpress.org/Function_Reference/register_post_type

    【讨论】:

      【解决方案2】:

      试试这个代码

      add_action( 'pre_get_posts', 'search_in_custom_post_type' );
      
      function search_in_custom_post_type( $query ) {
              if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
                    $query->set( 'post_type', array( 'country', 'location' ) );
               }   
      
               return $query;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 2016-08-15
        • 2013-12-22
        • 2014-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多