biggie
biggie

Reputation: 41

$queryResult = new WP_Query( $args ) unknown error

since I am totally new to php i struggle with one custom function, problem is in line $queryResult = new WP_Query( $args ); since when I comment it out with the rest of function, everything works. However when it is there, page will show me no results for reservation which is strange

so function here

function getListOfUsersOnLesson($lesson_id) {

    $result = '... nikto nie je prihlásený';

    $args = array (
            'post_type' => 'rsg_reservations',
            'pagination' => false,
            'posts_per_page' => '-1',
            'meta_key' => '_rsg_reservation_lesson_id',
            'meta_value' => $lesson_id,
    );

    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        $result = '';
        while ( $query->have_posts() ) {
            $query->the_post();

            $user_id = get_post_meta( get_the_ID(), '_rsg_reservation_user_id', true );
            $user_info = get_userdata($user_id);
            $user_email = $user_info->user_email;
            $username = get_user_meta( $user_id, 'first_name', true ).' '.get_user_meta( $user_id, 'last_name', true );

            $erroclass = '';
            if(reservationIsValid($lesson_id, $user_id)) $erroclass = '';
            else $erroclass = 'erroruser';

            $result.= '<div class="row">';
            $result.= '<div class="col-md-4 '.$erroclass.'">'.$username.'</div>';
            $result.= '<div class="col-md-6 '.$erroclass.'">'.$user_email.'</div>';
            $result.= '<div class="col-md-2"></div>';
            $result.= '</div>';
        }
    }
    wp_reset_postdata();

    return $result;

}

When NOT commented out When NOT commented out

When commented out, desired state When commented out, desired state

whole code is called by another method in a separate php file

$lesson_user_list = '';
            if(is_super_admin()) {
                $lesson_user_list = getListOfUsersOnLesson($lesson_id);
            }

Upvotes: 2

Views: 539

Answers (3)

biggie
biggie

Reputation: 41

Solved, that puece with error class seems to be a problem

Upvotes: 0

Nahid Hasan
Nahid Hasan

Reputation: 471

You may be you mess with query variable ( $queryResult and $query )

if ( $queryResult->have_posts() ) {
        $result = '';
        while ( $queryResult->have_posts() ) {
            $queryResult->the_post();

Upvotes: 1

random_user_name
random_user_name

Reputation: 26160

You execute a new WP_Query and loading it into $queryResult, but then do not use it (your code uses $query, which isn't defined / doesn't contain anything).

Change your code as follows:

// You are loading results into $queryResult here
$queryResult = new WP_Query( $args );
// below switched FROM $query->have_posts() TO $queryResult->have_posts()
if ( $queryResult->have_posts() ) {
    $result = '';
    // switch to $queryResult->have_posts()
    while ( $queryResult->have_posts() ) {
        // switch to $queryResult->the_post()
        $queryResult->the_post();
        // ... the rest of your code ...
    }
}

Upvotes: 0

Related Questions