Passing an array into WP_Query as a variable

I am trying to dynamically create a meta query based upon an array of post_id values. However, the query is not working. I have used var_dump() to see $meta_array, and it appears to be a standard array in the correct format to pass into WP_Query().

$post_id_array = array( “12”, “24” ); //this array will be dynamically generated
$meta_array = array();
foreach ($post_ids_array as $key => $value) {
array_push($meta_array,
array(
‘key’ => ‘relate_blog_posts’,
‘value’ => $value,
‘compare’ => ‘LIKE’
)
);
}

$post_args = array(
‘post_type’ => ‘post’,
‘posts_per_page’ => 3,
‘post_status’ => ‘publish’,
‘meta_query’ => array($meta_array)
);
$post_query = new WP_Query($post_args);

Please let me know if I have made a mistake, or am simply going about this the wrong way.

Thanks!

Read more here:: Passing an array into WP_Query as a variable

Leave a Reply

Your email address will not be published. Required fields are marked *