我的WP\\u查询有点困难,其中包括一个meta\\u查询,用于根据ACF用户字段限制帖子,但如果用户可以“编辑\\u帖子”,则显示所有帖子。从下面的代码中可以看出,前两个数组在以下位置工作良好:;
在ACF字段中选择当前用户ID;或ACF用户字段为空,因此值为“”;或当前用户功能为“编辑帖子”如果存在这三种情况中的任何一种,则相应地显示帖子。这是第三种情况,它不起作用,需要一些帮助。这是我目前的代码:
$user_exception = current_user_can(\'edit_posts\');
$current_user_id = get_current_user_id();
$query = new WP_Query( array(
\'post_type\' => \'promotions\',
\'posts_per_page\' => 10,
\'meta_key\' => \'promo_start_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'paged\' => $paged,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'dedicated_promotion\',
\'value\' => $current_user_id,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'dedicated_promotion\',
\'value\' => \'\',
\'compare\' => \'=\'
),
array(
\'key\' => \'wp_capabilities\', //This part is current not working as I intended
\'value\' => $user_exception,
\'compare\' => \'=\'
)
)
) );
?>
有人对如何检索当前登录的用户功能并将其添加到meta\\u查询中有什么建议吗?
SO网友:Rein
#SOLVED#通过使用if语句定义meta\\u查询条件可以解决这个问题。因此,如果current\\u user\\u can(\'subscriber\'),则meta\\u查询包括条件,否则查询包括所有帖子。
这是代码,以防对其他人有帮助:
<?php
//create variables
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$current_user_id = get_current_user_id();
//check if current user is a subscriber and define meta_query arguments
$args = array(
\'post_type\' => \'promotions\',
\'posts_per_page\' => 10,
\'meta_key\' => \'promo_start_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'paged\' => $paged
);
if (current_user_can( \'subscriber\' )) {
// Add meta_query
$args[\'meta_query\'] = array(
\'relation\' => \'OR\',
array(
\'key\' => \'dedicated_promotion\',
\'value\' => $current_user_id,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'dedicated_promotion\',
\'value\' => \'\',
\'compare\' => \'=\'
)
);
}
//set up the query
$query = new WP_Query($args);
if ( $query->have_posts() ) : ?>