I am trying to get meta value from GD Rating table for each post ID with following sql:

$querystr = "SELECT meta_value FROM $wpdb->gdrts_itemmeta WHERE meta_key LIKE 'stars-rating_rating'";
$ratings = $wpdb->get_results($querystr);
foreach ($ratings as $rating)
$ratingku = get_post_meta($post->ID, $rating, true);

But it failed, It return a word: "ARRAY".

How to get meta_value from another table (created by a plugin) for each post by using SQL or Query?

  • Is the problem $wpdb->gdrts_itemmeta? You didn't say what the failure was. I'd guess you'll need to construct the table name yourself from $wpdb->prefix . "gdrts_itemmeta".
    – Rup
    Commented Oct 2, 2019 at 13:07
  • 1
    How exactly did it fail? Did you try to dump the $ratings - var_dump( $ratings ); - and is it empty? Are there any errors? And is that $rating->meta_value; a typo? Also, shouldn't the $ratingku be inside the foreach loop?
    – Sally CJ
    Commented Oct 2, 2019 at 16:50
  • It give empty result. I think it failed cause the result from querystr is not associated witf post ID.
    – panjianom
    Commented Oct 3, 2019 at 0:00
  • Actually, have you tried echo $rating->meta_value; ? And you could also just use wpdb::get_col() - $ratings = $wpdb->get_col( $querystr ); - this way, you'd get an array of meta values and not an array of meta object. (I.e. You could simply echo $rating; in the foreach)
    – Sally CJ
    Commented Oct 3, 2019 at 5:09
  • 1
    The issue have been solved.
    – panjianom
    Commented Oct 3, 2019 at 7:32


Your Answer

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

Browse other questions tagged or ask your own question.