I’m setting up a system to show customer reviews on a WordPress site. We have 6 service pages and I want to be able to choose when creating the review which of the service pages it shows on. I only want one random review that matches the relevant page to show each time.
I’ve created a custom post type (‘review’), added fields for review text, reviewer name, etc. I’ve also added an ACF Page Link field which allows me to choose the page or pages I want the review to show on. This is where I’m a bit stuck. I basically want the query to only get posts that match the Page Link selection. I can make it do the comparison later during the loop quite easily, but I can’t work out how to set the arguments to filter down in this way.
This is what I currently have:
global $post;
$args = array(
'post_type' => 'review',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand',
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
echo '<div class="review-block">';
while ( $loop->have_posts() ) : $loop->the_post();
$page_to_display_on = get_field ('page_to_display_on');
$reviewer_name = get_field ('reviewer_name');
$service_supplied = get_field ('service_supplied');
$reviewer_image = get_field ('reviewer_image');
$review_text = get_field ('review_text');
$review_stars = get_field ('review_stars');
echo $current_url;
echo '<div class="review-image">';
if ( $reviewer_image ) {
echo '<img class="review-image-circle" src="' . $reviewer_image . '" />';
}
else {
echo '<img class="review-image-circle" src="' . get_stylesheet_directory_uri() . '/images/google-reviews-icon.png" />';
}
echo '</div>';
echo '<div class="review-content">';
echo '<p>' . $review_text . '</p>';
echo '<p class="review-name">' . $reviewer_name . ' - ' . $service_supplied . '</p>';
for($x=1;$x<=$review_stars;$x++) {
echo '<i class="fa fa-star" aria-hidden="true"></i>';
}
if (strpos($review_stars,'.')) {
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
$x++;
}
while ($x<=5) {
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
$x++;
}
echo '</div>';
endwhile;
echo '</div>';
}
wp_reset_postdata();
Is there a way to do a relationship query with an ACF custom field? Would I be better using a ‘Post Object’ field in ACF, which can return the post ID instead?