php - How to return data in loop for WP REST API? -
function getprogramtourinfobyid(){ $data = getprogramtourinfobycate(); //print_r($data); foreach ($data $key => $id) { //echo( $id); $count =0; while( have_rows('package_scd', $id ) ): the_row(); //if(get_sub_field('start_date') != null){ //echo $id."\r\n"; $array[$count] = array( 'id' => $id , 'title' => get_the_title($id), 'url' => get_the_permalink($id), 'start' => get_sub_field('start_date'), 'end' => get_sub_field('end_date'), 'price_single_room' => get_sub_field('price_single_room'), 'price_adult' => get_sub_field('price_adult'), 'type' => "restricted", ); $count ++; endwhile; } $output = array('sourcename' => $array ); return $output; } function getprogramtourinfobycate(){ $term_id = 3; $args = array( 'post_type' => array( 'package'), 'posts_per_page' => -1, 'post_status' => array('publish') , 'tax_query' => array( array( 'taxonomy' => 'tour-categories', 'field' => 'id', 'terms' => $term_id, 'order'=>'desc', 'orderby'=>'menu_order', ) ) ); $query = new wp_query($args); if($query->have_posts()): while($query->have_posts()): $query->the_post(); $arr[] = get_the_id(); endwhile; return $arr; endif; }
here code return wrong output. when test echo $id
in loop while( have_rows('package_scd', $id ) ): the_row()
there return of id database right output:
but when have return $output = array('sourcename' => $array )
. there return 2 id (24930 , 18367) not sure why rest of id not coming.
may overwrite $array keys.. declare,
$count = 0; $array = array();
before foreach loop starts. sure, work.
Comments
Post a Comment