wp query - All posts returned when author of 0 is queried

I have a small problem when attempting to query some posts by author.I have events and registrations. Currently I have

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
Add a comment  | 

2 Answers 2

Reset to default 0

Welcome 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347672a4426876.html

相关推荐

  • wp query - All posts returned when author of 0 is queried

    I have a small problem when attempting to query some posts by author.I have events and registrations. Currently I have

    21小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信