user2593040 user2593040 - 6 months ago 28
SQL Question

Get array of ID's then Query different post type by those ID's

I am needing some assistance. My goal is to query "clinics" post type by array of zip codes. Then get the ID's of those clinics and run another query of post type called "clinicspromo" to get those results. Then inside the loop, run query #3 to retrieve the clinic information again that is tied to each clinic promo. I have most of it completed, I am just having a problem turning the results of $post->ID; to an array of ID's separated by commas just like the zip code list. Any help would be appreciated!

$zipcodelist = '90001, 90002, 90003'; // Only for demo purposes

/* QUERY CLINICS POST TYPE TO GET LIST OF CLINICS BY ZIP CODES */
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );

// Trying this but not sure if anything below is correct
$postlist = get_posts( $args );

$posts = array();
foreach ( $postlist as $post ) {

// I did this to try and save an array of ID's, but doesn't work
$posts[] += $post->ID;
}

//This is suppose to get the posts ID's and save to variable
$current = array_search( get_the_ID(), $posts );

/* QUERY #2 - QUERY "clinicpromos" POST TYPE BY ARRAY OF ID'S. Except the
ID's aren't coming over, returns "0" if I try to echo */
$argstwo = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinicpromos',
'meta_query' => array (
array (
'meta_key' => 'assignclinic',
'meta_value' => $current,
'compare' => 'IN'
)
) );

/* the query for second Query, but will I have to query clinics
again to get the address, phone etc.. of the clinics post type?*/

$the_query = new WP_Query( $argstwo );

// Then The Loop

Answer

Changed your foreach loop like: I think you have an extra + sign when storing the IDs.

This is your loop:

foreach ( $postlist as $post ){
  $posts[] += $post->ID; 
}

Replace it with:

foreach ( $postlist as $post ){
  $posts[] = $post->ID; 
}
Comments