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';

 $args = array(   
'orderby'          => 'post_title',
'order'            => 'DESC', 
'post_type' => 'clinics',
   'meta_query' => array (
        array (
          'key' => 'cliniczipcode',
          'value' => $zipcodelist,
          'compare' => 'IN'
        )
      ) );


  $postlist = get_posts( $args );

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


$current = array_search( get_the_ID(), $posts );  


 $argstwo = array(   
'orderby'          => 'post_title',
'order'            => 'DESC', 
'post_type' => 'clinicpromos',
   'meta_query' => array (
        array (
          'meta_key' => 'assignclinic',
          'meta_value' => $current,
          'compare' => 'IN'
        )
      ) ); 


 $the_query = new WP_Query( $argstwo ); 

Related posts

2 comments

  1. 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; 
    }
    
  2. Try this, you do not need +

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

Comments are closed.