我有自定义用户角色“广告商”,并希望排除他们的帖子“由广告商角色的用户”从首页。
发布于 2020-11-02 20:41:17
例如,使用类别"Advertisement“并使用pre_get_posts从查询中排除该类别会更容易。广告商每次发布新帖子时,都需要添加此类别。
例如
add_action('pre_get_posts', 'front_page_pre_get_posts');
function front_page_pre_get_posts($query) {
if(is_front_page() && $query->is_main_query()) {
$query->set('category__not_in', array(1)); // Replace 1 by the category ID
}
}我知道这不是你具体问题的答案。这更像是一个建议。我将测试一些代码来做你想做的事情,然后我会用它更新我的答案。
更新
没有经过测试,但是像这样的东西应该可以工作。
query on >在pre_get_posts
中排除这些作者
add_action('pre_get_posts', 'front_page_pre_get_posts');
function front_page_pre_get_posts($query) {
// Get the users related to your role
$args = array(
// The role you want to exclude in your get post
'role' => 'advertiser',
// Will return an array of ids
'fields' => 'ids'
);
$users = get_users($args);
if(is_front_page() && $query->is_main_query()) {
// To exclude authors you can add a "-" before the id id of the author
$query->set('author', '-'.implode(',-', $users)); // Will generate a string like "-1,-45,-48"
}
}https://stackoverflow.com/questions/64643753
复制相似问题