2015-09-10 11 views
12

Ich kann bereits deaktivieren (entfernen Sie die Besonderheiten von normalen Posts) in der JSON von der WordPress API zurückgegeben. Ich benutze die unten aus diesem Beispiel folgenden: https://css-tricks.com/using-the-wp-api-to-fetch-posts/Unset Daten von WordPress API (Wp-JSON)

Was Ich habe Probleme mit und kann nicht herausfinden, ist, wie man dies zu ändern, so führt zum Löschen es Daten von einem Gewohnheit Beitrag Typ

Gedanken?

function qod_remove_extra_data($data, $post, $context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($context !== 'view' || is_wp_error($data)) { 
    return $data; 
    } 

    // Here, we unset any data we don't want to see on the front end: 
    unset($data['author']); 
    unset($data['status']); 
    unset($data['featured_image']); 
    //etc etc 

    return $data; 
} 

add_filter('json_prepare_post', 'qod_remove_extra_data', 12, 3); 

benutzerdefinierte Post-Typ Beispiel Filter:

function projectPost_remove_extra_data($data, $post, $context) { 

    if ($context !== 'view' || is_wp_error($data)) { 
    return $data; 
    } 

    // Here, we unset any data we don't want to see on the front end: 
    unset($data['author']); 



    return $data; 
} 

add_filter('json_prepare_project', 'projectPost_remove_extra_data', 12, 3); 
+0

Welche API-Version verwenden Sie? Es ändert sich für v2. Bitte sehen Sie: https://github.com/WP-API/WP-API/issues/1195 – brianlmerritt

+0

@brainlmeritt Ich verwende Version 1 – RMH

Antwort

3

Für wp-api v1.x, müssen Sie WP_JSON_CustomPostType verlängern. Es gibt ein Beispiel in den Seiten-Datei (class-wp-json-pages.php)

<?php 
/** 
* Page post type handlers 
* 
* @package WordPress 
* @subpackage JSON API 
*/ 

/** 
* Page post type handlers 
* 
* This class serves as a small addition on top of the basic post handlers to 
* add small functionality on top of the existing API. 
* 
* In addition, this class serves as a sample implementation of building on top 
* of the existing APIs for custom post types. 
* 
* @package WordPress 
* @subpackage JSON API 
*/ 
class WP_JSON_Pages extends WP_JSON_CustomPostType { 
    /** 
    * Base route 
    * 
    * @var string 
    */ 
    protected $base = '/pages'; 

    /** 
    * Post type 
    * 
    * @var string 
    */ 
    protected $type = 'page'; 

    /** 
    * Register the page-related routes 
    * 
    * @param array $routes Existing routes 
    * @return array Modified routes 
    */ 
    public function register_routes($routes) { 
     $routes = parent::register_routes($routes); 
     $routes = parent::register_revision_routes($routes); 
     $routes = parent::register_comment_routes($routes); 

     // Add post-by-path routes 
     $routes[ $this->base . '/(?P<path>.+)'] = array(
      array(array($this, 'get_post_by_path'), WP_JSON_Server::READABLE), 
      array(array($this, 'edit_post_by_path'), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON), 
      array(array($this, 'delete_post_by_path'), WP_JSON_Server::DELETABLE), 
     ); 

     return $routes; 
    } 

    /** 
    * Retrieve a page by path name 
    * 
    * @param string $path 
    * @param string $context 
    * 
    * @return array|WP_Error 
    */ 
    public function get_post_by_path($path, $context = 'view') { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->get_post($post['ID'], $context); 
    } 

    /** 
    * Edit a page by path name 
    * 
    * @param $path 
    * @param $data 
    * @param array $_headers 
    * 
    * @return true|WP_Error 
    */ 
    public function edit_post_by_path($path, $data, $_headers = array()) { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->edit_post($post['ID'], $data, $_headers); 
    } 

    /** 
    * Delete a page by path name 
    * 
    * @param $path 
    * @param bool $force 
    * 
    * @return true|WP_Error 
    */ 
    public function delete_post_by_path($path, $force = false) { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->delete_post($post['ID'], $force); 
    } 

    /** 
    * Prepare post data 
    * 
    * @param array $post The unprepared post data 
    * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent) 
    * @return array The prepared post data 
    */ 
    protected function prepare_post($post, $context = 'view') { 
     $_post = parent::prepare_post($post, $context); 

     // Override entity meta keys with the correct links 
     $_post['meta']['links']['self'] = json_url($this->base . '/' . get_page_uri($post['ID'])); 

     if (! empty($post['post_parent'])) { 
      $_post['meta']['links']['up'] = json_url($this->base . '/' . get_page_uri((int) $post['post_parent'])); 
     } 

     return apply_filters('json_prepare_page', $_post, $post, $context); 
    } 
} 

Ersetzen "Seiten" mit "MyCustomPostTypes" und der Seite mit "mycustomposttype". Nur vorsichtig sein, nicht interne Wordpress-Code umzubenennen, die auch den Begriff page

verwendet Hinweis: wahrscheinlich das am besten als Plugin hinzufügen, anstatt das Plugin JSON-WP-API

/** 
* Plugin Name: MyCustom JSON App API 
* Description: MyCustomPost handler for the JSON API 
* Dependency: This plugin requires JSON-WP-API Plugin!!!! 
* Author: 
* Author URI: 
* Version: 
* Plugin URI: 
*/ 
1

Es sollte nicht geändert werden anders, um Daten aus benutzerdefinierten Post-Typen als aus den integrierten Post-Typen zu entfernen. Haben Sie bestätigt, dass Ihr API-Aufruf tatsächlich Ihre CPTs zurückgibt? Zuerst sollten Sie sich den Wert dessen anschauen, was von: http://yourwebsite.com/wp-json/posts/types zurückgegeben wird. Unter der Annahme, dass Ihr CPT-Typ dort auftaucht, sollten Sie in der Lage sein, nach Elementen dieses Typs, z.B. product, durch den Anruf: http://yourwebsite.com/wp-json/posts?type=product.

Mit anderen Worten, Sie sollten nicht den Namen des Filters ändern: Sie möchten immer noch in json_prepare_post binden. Wenn Sie möchten, dass Ihre Filter empfindlich machen Art zu schreiben und nur bestimmte Felder entfernen, wenn Sie eine CPT haben so etwas wie Sie tun können:

function my_remove_extra_product_data($data, $post, $context) { 
    // make sure you've got the right custom post type 
    if ('product' !== $data[ 'type' ]) { 
     return $data; 
    } 
    // now proceed as you saw in the other examples 
    if ($context !== 'view' || is_wp_error($data)) { 
     return $data; 
    } 
    // unset unwanted fields 
    unset($data[ 'author' ]); 

    // finally, return the filtered data 
    return $data; 
} 

// make sure you use the SAME filter hook as for regular posts 
add_filter('json_prepare_post', 'my_remove_extra_product_data', 12, 3); 

können Sie weitere Dokumentation in der WP API Getting Started Guide finden.

+0

Ich aktualisierte mein Arbeitsbeispiel zu dem, was Sie aufgelistet, und es war nicht in der Lage, irgendwelche zu deaktivieren der Daten. Die API funktioniert, und ich habe sie tatsächlich mit einem Ajax-Aufruf gestartet, um den Titel, das ACF-Feld und den Inhaltsbereich zu erhalten. – RMH

+0

könnten Sie einen Link zu Ihrer Website bereitstellen? – morphatic

+0

ah, okay, das hört sich gut an. Fügen Sie in der Funktion als erste Zeile Folgendes hinzu: 'echo '

' . print_r($data) . '
'; exit; 'und führe es erneut aus. Dies sollte einen var-Dump der '$ data' -Variablen machen. Es ist sehr wahrscheinlich, dass wir entweder versuchen, Variablen zu löschen, die nicht existieren, oder dass wir versuchen, auf Objektelemente als Array-Indizes zuzugreifen. – morphatic

2

Wenn möglich, nur die Beispiele in Internet gezeigt ist:

qod_remove_extra_data function ($ data, $ post, $ context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($ context! == 'view' || is_wp_error ($ data)) { 
     return $ data; 
    } 
    // Here, we unset any data we do not want to see on the front end: 
    unset ($data ['author']); 
    unset ($data ['status']); 
    // Continue unsetting whatever other fields you want return $ data; 
} 
add_filter ('json_prepare_post' 'qod remove extra_data', 12, 3); 

und rechts ist:

qod_remove_extra_data function ($ data, $ post, $ context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($ context! == 'view' || is_wp_error ($ data)) { 
     unset ($data->data ['excerpt']); //Example 
     unset ($data->data ['content']); //Example 
     unset ($data->data ['name field to remove']) 
     //or 
     unset ($data->data ['name field to remove'] ['name subfield if you only want to delete the sub-field of field' ]) 
     return $data; 
    } 
} 
add_filter ('rest_prepare_post' 'qod_remove_extra_data', 12, 3); 

WICHTIG: ist: add_filter ('rest_prepare_post' ‚qod_remove_extra_data ', 12, 3);

Nicht: add_filter ('json_prepare_post' 'qod Entfernen extra_data', 12, 3); // FALSCH

Wenn ist Benutzerdefinierte Beitrag Typ: add_filter ('rest_prepare _ {$ post_type}' 'qod_remove_extra_data', 12, 3);

BEISPIEL: Name Beitragstyp = Produkt; add_filter ('rest_prepare_product' 'qod_remove_extra_data', 12, 3);

Mit diesem Code können Sie die Felder, die Sie möchten, die JSON entfernen. Durch die Verwendung von rest_prepare} _ {$ post_type entscheiden Sie, dass Sie alle post_type-Felder gelöscht haben. Dies betrifft nur den gewünschten post_type und nicht alle.