Я хотел бы ограничить доступ к трем типам сообщений: "service", "comment" и "reply".
Я хочу создать настраиваемые поля "service_allowedUsersId", "comment_allowedUsersId" и "reply_allowedUsersId" и отображать статьи только тогда, когда мой идентификатор включен.
Но следующий код кажется избыточным.
Пожалуйста, дайте мне знать, если есть лучший способ написать.
$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.
спасибо.