Querying Custom Posts Using an Array
-
Hi,
On my single custom post page, I’d like to display a “More Like This” section, with up to 6 additional posts. These custom posts (of type “program”) all have an ACF checkbox with multiple possible values, and I’d like to use this to pull related posts.
For the sake of simplicity, let’s say the ACF checkbox variable name is “colors” and possible values are “red”, “green”, “yellow”, and “blue”.
I can get the ID of the current program, as well as an array with this program’s values for “colors”: “red”, “blue”, “yellow”.
How do I now query all the programs and pull a list of posts that have one or more of those array values for “colors”?
Here’s what I have so far:
$similar = get_field( ‘colors’); /* This is returning an array for the current program’s “color” array: “red”, “green”, “blue” */
$args = array( 'numberposts' => 6, 'post_type' => 'program', 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'color', 'value' => $similar 'compare' => 'IN') )); $more_like_this = new WP_Query ($args); while ($more_like_this->have_posts()) : $more_like_this->the_post(); ?> <?php the_title(); ?> <?php echo $star_rating ?> <?php endwhile; wp_reset_query(); ?>
I’m unable to figure out how to iterate through the array to see if it matches one of the values in $similar. I’m not getting any results. Of course, the tricky part, once I get results, is that I don’t want duplicates (as other programs may also have multiple colors in common with the current program).
I’ve been stuck on this for days–any help would be immensely appreciated. Thank you!
- The topic ‘Querying Custom Posts Using an Array’ is closed to new replies.