2016-03-31 10 views
1

Ich arbeite an WordPress JSON API-Plugin. Ich meine benutzerdefinierte Controller erstellt, möchte ich Beiträge als JSON-Format, meine Arbeit war, wie unten abgerufen werden:

Mein PHP-Code ist:

$rows = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where $wpdb->posts.post_status = 'publish' AND 
      $wpdb->posts.post_title LIKE '%%%s%%' ", $key)); 
      $count = 0 ; 
      foreach($rows as $post) {  
      $output[] = array('id' => $post->ID, 'title' => $post->post_title, 'price' =>$post->custom_fields->price); 
      ++$count ; 
      } 
     if($count == 0){ 
      $data = array ('status'=>'ok', 'count'=> $count , 'result'=> "No data found "); 
      }else 
     { 
      $data = array ('count'=> $count , 'result'=> $output); 
     } 
     header('Content-Type: application/json; charset=UTF-8'); 
     return $data; 
     } 

Json Ergebnis wie folgt:

{ 
"status": "ok", 
"count": 10, 
"result": [ 
    { 
    "id": "51", 
    "title": "a", 
    "price": null 
    }, 
    { 
    "id": "82", 
    "title": "b", 
    "price": null 
    }, 
    } 

Warum Preis ist auf Null gesetzt, was die richtige Syntax, um Preis von benutzerdefinierten Feldern aus Posts in WordPress zu extrahieren?

+1

Bitte formatieren Sie Ihren Code richtig. –

+0

Machen Sie eine 'print_r ($ rows)', und sehen Sie, was Sie innen haben. Aus Ihrem Ergebnis scheint $ post-> custom_fields-> price '' null' zu sein ... –

Antwort

0

danke dears für deine schnelle Hilfe, ich die Lösung gefunden, verwende ich musste: get_post_meta Funktion Preis abzurufen, so dass diese Zeile:

$output[] = array('id' => $post->ID, 'title' => $post->post_title,'price' =>$post->custom_fields->price); 

wird, wie folgend:

$output[] = array('id' => $post->ID, 'title' => $post->post_title, 'price' =>get_post_meta($post->ID, 'price', true)); 

danke,