I would like to restrict access to the three post types "service", "comment", and "reply".
I want to create custom fields "service_allowedUsersId", "comment_allowedUsersId", and "reply_allowedUsersId" and display articles only when my ID is included.
But the following code seems redundant.
Please let me know if there is a better way to write.
$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;
Specifying SQL is difficult, so it is best if you can use WP_Query.
thanks.