Get one post by post type and ID inside of a loop

I am trying to do a loop inside of a loop. But it’s not returning my values like it should. See Comments in the code. It explains it better. Any advice would be appreciated!

 /* LETS PRETEND I HAVE MY FIRST QUERY HERE */
 /* THEN THE LOOP */     
 while ( $the_query->have_posts() ) : $the_query->the_post();  

     // Establish ID of the clinic from clinicpromo custom field
     $clinicid = get_custom_field('assignclinic');  

    /* MY TROUBLE QUERY:*/

    /* GET CLINIC BY ID - This should only retrieve 1 clinic */
    $argsthree = array(   
      'ID' => $clinicid,
      'post_type' => 'clinics'
     );

  $clinics_array = get_posts( $argsthree ); 

   /* I AM TRYING TO QUERY THE CLINIC THAT THE PROMO BELONGS TO AND SAVE   
      ITS' CUSTOM FIELDS AND REGULAR FIELDS AS VARIABLES TO BE USED LATER */

    foreach ( $clinics_array as $clinic ) :  

      $clinictitle = get_the_title($clinicid);  
      $cliniccity = get_custom_field('cliniccity');
      $clinicstate = get_custom_field('clinicstate');  

  endforeach; 

 // How can I use these variables outside of the loop? Or is it possible?
 // I keep getting "ArrayArrayArray... as results

     echo $clinictitle;
     echo $cliniccity;
     echo $clinicstate; 

  /* These variables will be mixed with content from the first query of clinicspromo

/=============SIMPLIFIED UPDATE=========================/

    $clinicid = get_custom_field('assignclinic'); // This is the ID of the clinic I am retrieving from ClinicPromos 

     $clinicpost = get_post($clinicid,array('post_type=clinicpromos'));



      $clinictitle = get_the_title($clinicpost->ID);  
      $cliniccity = get_post_custom_values('cliniccity', $clinicpost->ID);
      $clinicstate = get_post_custom_values('clinicstate', $clinicpost->ID);  

     echo $clinictitle;
     echo $cliniccity;
     echo $clinicstate; 

Related posts

1 comment

  1. Try this way, since you are already getting the post id you do not need to do get post

    $clinicid = get_custom_field('assignclinic');
    

    you do not need this $clinicpost = get_post($clinicid,array('post_type=clinicpromos'));

      $clinictitle = get_the_title( $clinicid);  
      $cliniccity = get_post_meta($clinicid, 'cliniccity', true);
      $clinicstate = get_post_meta($clinicid,'clinicstate', true); 
    
     echo $clinictitle;
     echo $cliniccity;
     echo $clinicstate;
    

Comments are closed.