我想限制对“服务”,“评论”和“回复”这三种帖子类型的访问。
我想创建自定义字段“ service_allowedUsersId”,“ comment_allowedUsersId”和“ reply_allowedUsersId”,并仅在包含我的ID时显示文章。
但是以下代码似乎是多余的。
请告诉我是否有更好的书写方式。
$uid = get_displayed_user_id();
$args = array(
'posts_per_page' => 15,
'post_type' => array('service', 'comment', 'reply'),
'author' => $uid,
// Determine if you can view "service"
'meta_query' => array(
'relation'=>'OR',
array(
'key' => 'service_allowedUsersId',
'value' => get_current_user_id(),
'compare' => 'LIKE',
'type'=>'NUMERIC'
),
array(
'key' => 'service_allowedUsersId',
'value' => '',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'service_allowedUsersId',
'value' => '',
'compare' => '=',
),
),
// Determine if you can view "comment"
'meta_query' => array(
'relation'=>'OR',
array(
'key' => 'comment_allowedUsersId',
'value' => get_current_user_id(),
'compare' => 'LIKE',
'type'=>'NUMERIC'
),
array(
'key' => 'comment_allowedUsersId',
'value' => '',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'comment_allowedUsersId',
'value' => '',
'compare' => '=',
),
),
// Determine if you can view "reply"
'meta_query' => array(
'relation'=>'OR',
array(
'key' => 'reply_allowedUsersId',
'value' => get_current_user_id(),
'compare' => 'LIKE',
'type'=>'NUMERIC'
),
array(
'key' => 'reply_allowedUsersId',
'value' => '',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'reply_allowedUsersId',
'value' => '',
'compare' => '=',
),
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<p>Of the articles that Mr. "' . $uid . '" wrote, here is what you can view.</p>';
echo '<p>' .the_ttile() .'</p>';
endwhile;
endif;
指定SQL很困难,因此最好使用WP_Query。
谢谢。