0

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();
    //}

?>
1
  • unfortunately checking the login is only option here. Commented Jun 15, 2020 at 15:03

2 Answers 2

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.

2
  • Thanks for your reply. Didn't know about PHP outputting var values within double quotes. I did indeed get this working but was trying not to have too many ifs. So I was just searching for the current logged on user, which would be 0 if not logged on. Would you suggest checking for userID !== 0 or use function is_user_logged_on()?
    – PeterB
    Commented Jun 15, 2020 at 16:13
  • Querying with a 0 value is the problem as I see it.
    – jdm2112
    Commented Jun 15, 2020 at 16:24
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.