从 Wordpress 搜索中排除多个自定义分类术语 [英] Exclude multiple custom taxonomies terms from Wordpress search

查看:68
本文介绍了从 Wordpress 搜索中排除多个自定义分类术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Wordpress 搜索结果中排除了任何将自定义分类法设置为特定术语的帖子或自定义帖子.我希望能够简单地添加更多分类法和术语(如在数组中),而无需重复该函数,并确保我能有效地执行此操作.

I am excluding from Wordpress search results any posts or custom posts with custom taxonomies set to specific terms. I want to be able to add more taxonomies and terms simply (like in an array) without having the duplicate the function, and ensure I'm doing it efficiently.

谁能建议一个更干净的函数来适应这个?

Can anyone suggest a cleaner function that accommodates this?

/* Exclude from WordPress Search using custom taxonomy */
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Exclude Terms by ID from Search and Archive Listings
    if ( is_search() || is_tax( 'marque' ) ) {    
        $tax_query = array([
            'taxonomy' => 'site_search',
            'field' => 'term_id',
            'terms' => [ exclude_page ],
            'operator' => 'NOT IN',
        ]);

        $query->set( 'tax_query', $tax_query );
    }
}, 11, 1 );


/* Exclude from WordPress Search using custom taxonomy */
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Exclude Terms by ID from Search and Archive Listings
    if ( is_search() || is_tax( 'marque' ) ) {    
        $tax_query = array([
            'taxonomy' => 'job_status',
            'field' => 'term_id',
            'terms' => [ closed ],
            'operator' => 'NOT IN',
        ]);

        $query->set( 'tax_query', $tax_query );
    }
}, 11, 1 );

推荐答案

您可以先尝试先将数组中的所有数据定义为分类法/术语对 (我已将数组嵌入到外部函数中,但是可以直接在hooked函数中添加).这样您就可以轻松添加或删除数据.

You could try first to define all your data in an array first as taxonomies / terms pairs (I have embedded the array in an external function, but it can be added directly in the hooked function). This way you can add or remove data easily.

然后我们使用foreach循环来读取和设置税务查询中的数据.所以你的代码将类似于:

Then we use a foreach loop to read and set the data in the tax query. So your code will be something like:

// HERE set in the array your taxonomies / terms pairs
function get_custom_search_data(){
    return [
        'site_search' => [ 'exclude_page' ],
        'job_status'  => [ 'closed' ],
    ];
}

/* Exclude from WordPress Search using custom taxonomy */
add_action( 'pre_get_posts', 'multiple_taxonomy_search', 33, 1 );
function multiple_taxonomy_search( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Exclude Terms by ID from Search and Archive Listings
    if ( is_search() || is_tax( 'marque' ) ) {
        // Set the "relation" argument if the array has more than 1 custom taxonomy
        if( sizeof( get_custom_search_data() ) > 1 ){
            $tax_query['relation'] = 'AND'; // or 'OR'
        }

        // Loop through taxonomies / terms pairs and add the data in the tax query
        foreach( get_custom_search_data() as $taxonomy => $terms ){
            $tax_query[] = [
                'taxonomy' => $taxonomy,
                'field' => 'slug', // <== Terms slug seems to be used
                'terms' => $terms,
                'operator' => 'NOT IN',
            ];
        }

        // Set the defined tax query
        $query->set( 'tax_query', $tax_query );
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.未经测试,它应该可以工作.

Code goes in function.php file of your active child theme (or active theme). Untested, it should work.

这篇关于从 Wordpress 搜索中排除多个自定义分类术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆