admin管理员组

文章数量:1323157

in there i have data postmeta i want to show

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

        $loop = new WP_Query( 
                array(
                        'post_type'    => 'product'
                        // 'showposts'    => 4,
                        // 'meta_key'     => '_sale_price', 
                        // 'meta_value'   => '0', 
                        // 'meta_pare' => '>=',
                    )
                ); 
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;
}

in there i want to show data :

where the meta data have same post id data,

i want to loop the data in inside details, have someone help me or tell me what code need i improve so my code its work ?

in there i have data postmeta i want to show

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

        $loop = new WP_Query( 
                array(
                        'post_type'    => 'product'
                        // 'showposts'    => 4,
                        // 'meta_key'     => '_sale_price', 
                        // 'meta_value'   => '0', 
                        // 'meta_pare' => '>=',
                    )
                ); 
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;
}

in there i want to show data :

where the meta data have same post id data,

i want to loop the data in inside details, have someone help me or tell me what code need i improve so my code its work ?

Share edited Oct 24, 2016 at 1:04 APSB asked Oct 22, 2016 at 2:06 APSBAPSB 5874 gold badges11 silver badges29 bronze badges 2
  • Wait, so you just wanna get this as JSON? – Jeremy Jackson Commented Oct 22, 2016 at 2:20
  • sure.. have u can help me ? – APSB Commented Oct 22, 2016 at 3:36
Add a ment  | 

3 Answers 3

Reset to default 5

Meta data is storing in wp_postmeta table. Use json_encode.

You can fetch meta by $meta = get_post_meta( get_the_ID() );

Please check following code .

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

If you just want your table data as json, then you just need to use json_encode()

http://php/manual/en/function.json-encode.php

You can try this

if( $loop->have_posts() ) :

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

        $meta[] = array(
            "id"         => get_the_ID(),
            "post_name"  => get_the_title(),
            "_edit_lock" => get_post_meta( get_the_ID(), '_edit_lock', true ),
            "_edit_last" => get_post_meta( get_the_ID(), '_edit_last', true ),
            "username"   => get_post_meta( get_the_ID(), 'username', true ),
            "password"   => get_post_meta( get_the_ID(), 'password', true ),
            "email"      => get_post_meta( get_the_ID(), 'email', true ),
            "phone"      => get_post_meta( get_the_ID(), 'phone', true ),
        );

    endwhile;
endif;

echo  json_encode($meta);

本文标签: javascriptHow to display postmeta key and value in same post id wordpress (JSON data)Stack Overflow