admin管理员组文章数量:1332865
I have a small problem when attempting to query some posts by author. I have events and registrations. Currently I have 4 registrations, all created by User2 with the wp-admin showing that correctly. If I run the following code with a user logged on it works. User2 shows all and User1 shows none. However, if no-one is logged on it returns all of the registrations. The user_id is being shown as 0 so it shouldn't find any of the registrations but it finds all of them, those for user 1 and those for user 2. Is there anyway I could stop this without having to check is_user_logged_in() each time.
Thanks
$reg_count=0;
//if (is_user_logged_in()){
echo "looking for registrations with author id of " . get_current_user_id();
$registrations = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => "registration",
'author' => get_current_user_id(),
'meta_key' => 'first_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key'=> 'event_id',
'compare' => 'LIKE',
'value' => '"' . get_the_id() . '"'
)
)
));
$reg_count = $registrations->found_posts;
echo "found " . $reg_count . " registrations wih author id of " . get_current_user_id();
//}
?>
I have a small problem when attempting to query some posts by author. I have events and registrations. Currently I have 4 registrations, all created by User2 with the wp-admin showing that correctly. If I run the following code with a user logged on it works. User2 shows all and User1 shows none. However, if no-one is logged on it returns all of the registrations. The user_id is being shown as 0 so it shouldn't find any of the registrations but it finds all of them, those for user 1 and those for user 2. Is there anyway I could stop this without having to check is_user_logged_in() each time.
Thanks
$reg_count=0;
//if (is_user_logged_in()){
echo "looking for registrations with author id of " . get_current_user_id();
$registrations = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => "registration",
'author' => get_current_user_id(),
'meta_key' => 'first_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key'=> 'event_id',
'compare' => 'LIKE',
'value' => '"' . get_the_id() . '"'
)
)
));
$reg_count = $registrations->found_posts;
echo "found " . $reg_count . " registrations wih author id of " . get_current_user_id();
//}
?>
Share
Improve this question
asked Jun 15, 2020 at 14:38
PeterBPeterB
72 bronze badges
1
- unfortunately checking the login is only option here. – Sabbir Hasan Commented Jun 15, 2020 at 15:03
2 Answers
Reset to default 0Welcome to WPSE. The condition you want to prevent is a ID of 0 so only run the query if it does not.
Also, DRY - don't repeat yourself. Rather than call get_current_user_id()
multiple times, call it once and store it.
$reg_count= 0;
$user_id = get_current_user_id();
if ( 0 !== $user_id ) {
echo "looking for registrations with author id of $user_id"; //PHP will output var value in double quotes
$registrations = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => "registration",
'author' => $user_id,
'meta_key' => 'first_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_id',
'compare' => 'LIKE',
'value' => '"' . get_the_id() . '"' //Why not just get_the_id()?
),
),
));
echo "found $registrations->found_posts registrations with author id of $user_id";
}
NOTE: Not at my desk - the code above is untested. Comment here if there is a problem.
EDIT: added missing closing bracket for IF
statement.
An author
value of 0
results in the query skipping that parameter. See this question and specifically this answer.
Checking if the user is logged in would still be your best bet, since that would prevent unnecessary database queries from running.
本文标签: wp queryAll posts returned when author of 0 is queried
版权声明:本文标题:wp query - All posts returned when author of 0 is queried 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347671a2457854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论