Gets Post ID and Meta Value in an array via wpdb

I am creating a function which would return the the values of a meta key for all posts. Till now I have been able to get the meta value for all posts with the help of Stack-overflow. Now I also want to get the post id from which the meta value is retrieved. So that I can track which of meta value belongs to which posts. If I var_dump my function, I get this response.

array(2) { [0]=> string(22) "a:2:{i:0;i:3;i:1;i:1;}" [1]=> string(14) "a:1:{i:0;i:1;}" }

The ideal situation would be if I could get the post id in the place of the array key. So lets say for the post with id 6607 it should be something like

array(1) { [6607]=> string(22) "a:2:{i:0;i:3;i:1;i:1;}" }

I have tried using LEFT JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->postmeta.post_id but its not working. It returns me the same value I get without this line.

The Function

function wp_postquiz_total_completed_quizes_by_user() {

    global $wpdb;

    $mylink = $wpdb->get_results("
                    SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta
                    WHERE $wpdb->postmeta.meta_key = '_pq_users_answered_quiz_on_post'",
                    ARRAY_A);


    return $mylink;
}

Thanks in advance for the help ..


Source: wpdb

Leave a Reply