更新:我有以下代码工作。它从搜索结果中排除分类法。但是,我希望只有在用户未登录时才会发生这种情况。以下代码对登录和注销的用户生效。如何检查是否有人从功能登录。php文件?我试过了!\\u user\\u logged\\u()多次,但我似乎无法使其正常工作?谢谢你的帮助!
add_filter( \'pre_get_posts\', \'exclude_pages_search_when_logged_out\' );
function exclude_pages_search_when_logged_out($query) {
if ( $query->is_search && !is_user_logged_in() ) {
$tax_query = array([
\'taxonomy\' => \'wpfc_service_type\',
\'field\' => \'term_id\',
\'terms\' => [ 505 ],
\'operator\' => \'NOT IN\',
]);
$query->set( \'tax_query\', $tax_query );
}
return $query;
}
原始问题
I\'m trying to exclude the taxonomy \'wpfc_service_type\', term 505, from my search results and archive pages. I\'ve added the following to my functions.php file, but it doesn\'t seem to be working. What am I doing wrong?
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( \'wpfc_service_type\' ) ) {
$tax_query = array([
\'taxonomy\' => \'wpfc_service_type\',
\'field\' => \'term_id\',
\'terms\' => [ 505 ],
\'operator\' => \'NOT IN\',
]);
$query->set( \'tax_query\', $tax_query );
}
}, 11, 1 );