【问题标题】:Custom taxonomy category exclude from search自定义分类类别从搜索中排除
【发布时间】:2021-05-27 06:58:42
【问题描述】:

您好,我正在尝试从搜索查询中排除自定义帖子类别(特定类别 ID)。我的自定义帖子类别分类名称是review-cat。我想排除这个自定义帖子分类的特定 ID。

function wcs_exclude_category_search( $query ) {

    if ( $query->is_search ) {
      $query->set( 'cat', '-33 -46' );
      $query->set( 'post_type', array( 'review' ) );
      
    }
    return $query; 

  
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
  

【问题讨论】:

    标签: php wordpress post custom-taxonomy


    【解决方案1】:

    我找到了这个。可能对你有帮助https://stackoverflow.com/a/52140116/1053190

    或在https://gist.github.com/billerickson/1332274下面的代码复制/粘贴

    /**
     * Exclude term from search query
     * @author Bill Erickson
     * @link http://www.billerickson.net/customize-the-wordpress-query/
     *
     * @param object $query
     */
    function be_modify_search_query( $query ) {
        global $wp_the_query;
        if( $query === $wp_the_query && $query->is_search() ) {
            $tax_query = array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'hidden',
                    'operator' => 'NOT IN',
                )
            );
            $query->set( 'tax_query', $tax_query );
        }
    }
    add_action( 'pre_get_posts', 'be_modify_search_query' );
    

    【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2021-06-02
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2016-10-01
    相关资源
    最近更新 更多