0

I have a rating system for our products but there is an issue:

by the code, that will SELECT all comments in commentmeta table that have rating as meta_key. even those who do not have a rate (the user did comment but did not leave a rating for the product).
so I need to SELECT just those comments that the rating as meta_key is not NULL.
my current code:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmeta.comment_id = {$wpdb->prefix}comments.comment_ID 
    WHERE comment_post_ID = %d AND meta_key = 'rating' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );

Thanks for helping me

0

I founded myself my searching:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmeta.comment_id = {$wpdb->prefix}comments.comment_ID 
    WHERE comment_post_ID = %d AND meta_key = 'rating' AND meta_value IS NOT NULL AND meta_value <> '' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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