0

in there i want to display my data. when i try to display my data without postmeta its work but when i try to improve with postmeta i get blank response

$command = $_GET['command'];
switch ($command) {
    case 'list_product':

        $loop = new WP_Query( 
                array(
                        'post_type'    => 'product'
                        // 'showposts'    => 4,
                        // 'meta_key'     => '_sale_price', 
                        // 'meta_value'   => '0', 
                        // 'meta_compare' => '>=',
                    )
                ); 
if($loop->have_posts()) :

    $data = array( "api_status" => 1, "api_message" => "success");
    while ( $loop->have_posts() ) : $loop->the_post();

           $data[] = array("id" => get_the_ID(),
          "post_name" => get_the_title(),
          "post_meta" => get_post_meta(get_the_ID());

    endwhile;


    echo  json_encode($data);
break;
}

have someone help me what a improvement i need in my code, so my code can work like what i need ?

APSB
  • 577
  • 4
  • 11
  • 28

3 Answers3

0

Put the $data = ... before your if and add a results key with an empty array value to it. Put your echo after your endif.

In your while, add the results to that key using $data['results'][] = ...


Edit: spell it out a bit. If your results are empty then something else is wrong.

switch ($_GET['command']) {
    case 'list_product':

        $loop = new WP_Query( 
            array(
                'post_type'    => 'product',
                // 'showposts'    => 4,
                // 'meta_key'     => '_sale_price', 
                // 'meta_value'   => '0', 
                // 'meta_compare' => '>=',
            )
        );
        $data = array(
            'api_status' => 1,
            'api_message' => 'success',
            'results' => array(),
        );
        while ($loop->have_posts()) {
            $loop->the_post();
            $data['results'][] = array(
                'id' => get_the_ID(),
                'post_name' => get_the_title(),
                'post_meta' => get_post_meta(get_the_ID()),
            );
        }
        echo json_encode($data);
        break;
}
Walf
  • 8,535
  • 2
  • 44
  • 59
0

Try this:

$command = $_GET['command'];
switch ($command) {
    case 'list_product':

        $loop = new WP_Query(
            array(
                'post_type'    => 'product'
// 'showposts'    => 4,
// 'meta_key'     => '_sale_price',
// 'meta_value'   => '0',
// 'meta_compare' => '>=',
            )
        );

        if($loop->have_posts()) {

            $data = array( "api_status" => 1, "api_message" => "success");
            while( $loop->have_posts() ) {
                $loop->the_post();
                $data[] =   array(
                    "id" => get_the_ID(),
                    "post_name" => get_the_title(),
                    "post_meta" => get_post_meta(get_the_ID())
                );
            }

            /**
            $data[] =   array(
                "id" => get_the_ID(),
                "post_name" => get_the_title(),
                "post_meta" => get_post_meta(get_the_ID())
            );
             */

        }

        echo  json_encode($data);
        break;
}
M. Furqan
  • 31
  • 3
  • Undefined index when no results. – Walf Oct 24 '16 at 05:30
  • I don't know about wordpress but I did see a missing "endif" in his statement. Due to my low point I can't add comment directly to question. OP must understand how to enable error debugging in first place – M. Furqan Oct 24 '16 at 05:34
  • I have updated my answer.. I don't really know what to expect from those custom defined wordpress methods so $data[] could go inside or outside while clause. I just tried to fix syntax – M. Furqan Oct 24 '16 at 05:39
  • `$data[]` could not go outside `while`. Initial `$data = ` must be outside the `if` scope. – Walf Oct 24 '16 at 05:42
-1

Please try with below code:

$loop = new WP_Query(
   array(
       'post_type'    => 'post'
   )
);

$data = array( "api_status" => 1, "api_message" => "success");
if($loop->have_posts()) {
 while( $loop->have_posts() ) {
     $loop->the_post();
     $data[] =   array(
         "id" => get_the_ID(),
         "post_name" => get_the_title(),
         "post_meta" => get_post_meta(get_the_ID())
     );
 }
}
echo  json_encode($data);
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57