Question

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

Was it helpful?

Solution

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 );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top