在循环内部通过帖子类型和ID获取一篇帖子

时间:2016-05-18 07:45:11

标签: php wordpress foreach while-loop

我想在循环中做一个循环。但它并没有像我们应该那样回归我的价值观。请参阅代码中的注释。它解释得更好。任何意见,将不胜感激!

 /* 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

/ =============简化更新========================= /

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

1 个答案:

答案 0 :(得分:2)

尝试这种方式,因为您已经获得了帖子ID,所以不需要发帖

$clinicid = get_custom_field('assignclinic');

您不需要此$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;