创建函数为角色指定数值:
function get_user_level($role){
switch ($role) {
case \'Contributor\':
return 1;
);
break;
case \'Author\':
return 2;
);
break;
case \'Editor\':
return 4;
break;
case \'Administrator\':
return 5;
break;
case \'Super Admin\':
return 6;
break;
default:
return 0; // default subscriber or other roles
break;
}
}
现在您可以不使用元查询:
$args = array( \'post_type\'=>\'gyo_kbs\', \'posts_per_page\'=>-1, \'orderby\' => \'menu_order\', \'order\' => \'ASC\'); // Removed meta query
$the_query = new WP_Query ( $args );
while ( $the_query -> have_posts() ) : $the_query -> the_post();
$allowed_role = get_post_meta( get_the_ID(), \'gyo_kb_roleview\', true ); //get the meta value of allowed role
if( get_user_level($_role) < get_user_level($allowed_role) ){ // $_role from your question
continue; // skip post if the user role is less than allowed role
}
echo \'<h1>\' . $post -> post_title . \'</h1>\';
endwhile;
wp_reset_postdata();
更新:如果可以在post meta中保存用户角色的数值,则可以执行以下操作:
$args = array( \'post_type\'=>\'gyo_kbs\', \'posts_per_page\'=>-1, \'orderby\' => \'menu_order\', \'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'gyo_kb_roleview\',
\'value\' => get_user_level($_role),
\'compare\' => \'>=\'
),
)
);