Php 从Wordpress搜索中排除多个自定义分类术语
我将从Wordpress搜索结果中排除任何自定义分类设置为特定术语的帖子或自定义帖子。我希望能够简单地(像在数组中)添加更多的分类法和术语,而不需要复制函数,并确保高效地执行 有人能推荐一种更干净的功能来适应这种情况吗Php 从Wordpress搜索中排除多个自定义分类术语,php,wordpress,search,custom-taxonomy,hook-wordpress,Php,Wordpress,Search,Custom Taxonomy,Hook Wordpress,我将从Wordpress搜索结果中排除任何自定义分类设置为特定术语的帖子或自定义帖子。我希望能够简单地(像在数组中)添加更多的分类法和术语,而不需要复制函数,并确保高效地执行 有人能推荐一种更干净的功能来适应这种情况吗 /* Exclude from WordPress Search using custom taxonomy */ add_action( 'pre_get_posts', function ( $query ) { if ( is_admin() || ! $query
/* 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 );
您可以首先尝试将数组中的所有数据定义为分类法/术语对(我已将数组嵌入到外部函数中,但可以直接添加到挂钩函数中)。通过这种方式,您可以轻松地添加或删除数据 然后,我们使用foreach循环读取和设置税务查询中的数据。因此,您的代码将类似于:
// 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 );
}
}
//这里在数组中设置分类法/术语对
函数get_custom_search_data(){
返回[
“站点搜索”=>[“排除页面”],
“作业状态”=>[“已关闭”],
];
}
/*使用自定义分类法从WordPress搜索中排除*/
添加操作('pre_get_posts','multiple_taxonomy_search',33,1);
函数多分类法搜索($query){
if(is_admin()| |!$query->is_main_query()){
返回;
}
//从搜索和存档列表中按ID排除术语
如果(is_search()| | is_tax('marque')){
//如果数组具有多个自定义分类法,请设置“relation”参数
if(sizeof(获取自定义搜索数据())>1){
$tax_query['relation']='和';//或'or'
}
//循环浏览分类法/术语对,并在税务查询中添加数据
foreach(以$taxonomy=>$terms的形式获取\u自定义\u搜索\u数据()){
$tax_query[]=[
“分类法”=>$taxonomy,
'field'=>'slug',//$terms,
'运算符'=>'不在',
];
}
//设置已定义的税务查询
$query->set('tax\u query',$tax\u query);
}
}
代码进入活动子主题(或活动主题)的function.php文件。未经测试,它应该可以工作