Update post meta with comment meta
-
Ok, so I know this question is relatively a simple one, but the answer is alluding me.
Essentially, a review plugin is saving reviews for listings on a site to the comment meta. I am trying to use FacetWP to automatically filter listing by a few parameters, one being review average. Problem is, as far as I know, wp query doesn’t pull comment meta. So even though there is meta data for the average rating, it won’t search that.
So I just need to take that comment meta and save it to the post meta for the listings, so I can then call it in FacetWP.
I was using the code below (and many variations of it…) to try and do this successfully… To no avail. Any help would be appreciated. I’m sure I’m just missing something painfully obvious or just making some simple stupid mistake. Thanks!
function send_average_rating_to_meta( $post_id ) { if ( empty( $post_id ) ) { $post_id = get_the_ID(); } $comments = get_comments( array( 'post_id' => $post_id, 'meta_key' => 'pixrating', 'status' => 'approve' ) ); if ( empty( $comments ) ) { return false; } $total = 0; foreach ( $comments as $comment ) { $current_rating = get_comment_meta( $comment->comment_ID, 'pixrating', true ); $total = $total + (double) $current_rating; } $average = $total / count( $comments ); update_post_meta( $post_id, 'pixrating_post', $average ); } add_action( 'updated_post_meta', 'send_average_rating_to_meta' );
The topic ‘Update post meta with comment meta’ is closed to new replies.