2015-09-04 33 views
8

Come posso ottenere tutti i post da un tipo di post personalizzato specifico con l'API REST WP (v1 o v2)? Sono molto nuovo a questo e sto cercando di capire come farlo.API REST WP recuperano i messaggi dal tipo di post

Attualmente sto usando WP REST API v2 e sono riuscito a recuperare un elenco di tutti i tipi di messaggi con questo

http://domain.com/wp-json/wp/v2/types 

e poi è riuscito a ottenere il tipo di messaggio che mi interessa con

http://domain.com/wp-json/wp/v2/types/the-icons-update 

Come ottengo tutti i post da quel tipo di contenuto specifico?

Ho provato con

http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update 

ma restituisce un array vuoto (suppongo che restituisce i messaggi predefiniti e sul mio sito ci sono solo i messaggi all'interno del tipo di messaggio personalizzato che sto cercando di recuperare).

Potrebbe esserci un problema con il modo in cui ho registrato il tipo di post?

function custom_post_type() { 
$labels = array(
    'name'    => _x('The Icons Update', 'post type general name'), 
    'singular_name'  => _x('The Icons Update', 'post type singular name'), 
    'add_new'   => _x('Add Page', 'magazine'), 
    'add_new_item'  => __('Add New Page'), 
    'edit_item'   => __('Edit Page'), 
    'new_item'   => __('New Page'), 
    'all_items'   => __('All Pages'), 
    'view_item'   => __('View Page'), 
    'search_items'  => __('Search Pages'), 
    'not_found'   => __('No Page found'), 
    'not_found_in_trash' => __('No Page found in the Trash'), 
    'parent_item_colon' => '', 
    'menu_icon'   => '', 
    'menu_name'   => 'The Icons Update' 
); 
$args = array(
    'labels'  => $labels, 
    'description' => 'Holds our projects and project specific data', 
    'public'  => true, 
    'menu_position' => 5, 
    'supports'  => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), 
    'has_archive' => true, 
    'taxonomies' => array('post_tag', 'category'), 
    'hierarchical' => false, 
    'query_var'  => true, 
    'queryable' => true, 
     'searchable' => true, 
    'rewrite'  => array('slug' => 'the-icons-update') 
); 
register_post_type('magazine', $args); 
flush_rewrite_rules(); 
} 
add_action('init', 'custom_post_type'); 

Qualsiasi aiuto con questo è molto apprezzato.

risposta

5

V'è un modo realmente girato e facile per v.2. Tutto quello che dovete fare è di inserire al vostro args matrice seguente: 'show_in_rest' => true

Esempio:

register_post_type('recipe', 
    array(
      'labels' => $labels, 
      'public' => true, 
      'menu_position' => 5, 
      'hierarchical' => false, 
      'supports' => $supports, 
      'show_in_rest' => true, 
      'taxonomies' => array('recipe-type', 'post_tag'), 
      'rewrite' => array('slug' => __('recipe', 'recipe')) 
    ) 
); 
+0

Dove lo aggiungo? Grazie. – Si8

+0

@ Si8 nel file 'functions.php';) –

+0

Voglio solo includere l'ID del post e questo è tutto. Funzionerebbe il sopra? Posso aprire una nuova domanda se giustificato. Grazie – Si8

2

register_post_type ('nome del tipo di post' ...) non il nome 'add_new'. Cambia il nome del tuo tipo di post in Magazine e controlla il risultato. Spero che sia d'aiuto.

+0

Grazie, ho fatto che il cambiamento ma purtroppo non ha risolto il mio problema.Sono tornato alla v1 del plugin API REST e con http://domain.com/wp-json/posts?type=magazine sono riuscito a recuperare i post da quel tipo di post specifico. Grazie – Jeff

2

Tornando indietro alla v1 del plugin API REST e con /wp-json/posts?type=name-of-post-type sono riuscito a recuperare i post da quel tipo di post specifico.

2

Per utilizzare v2 del plugin API REST:

Nel file del vostro tema functions.php, aggiungere il seguente per creare un endpoint di riposo:

add_action('init', 'add_myCustomPostType_endpoint'); 
function add_myCustomPostType_endpoint(){ 

    global $wp_post_types; 
    $wp_post_types['myCustomPostType']->show_in_rest = true; 
    $wp_post_types['myCustomPostType']->rest_base = 'myCustomPostType'; 
    $wp_post_types['myCustomPostType']->rest_controller_class = 'WP_REST_Posts_Controller'; 
} 

Ora si dovrebbe avere la seguente endpoint interrogare da:

/wp-json/wp/v2/myCustomPostType 

myCustomPostType essendo pes om post type che hai registrato. "Rest_base" non deve corrispondere al nome del tuo tipo di post personalizzato.

Molto probabilmente vorrai aggiungere campi aggiuntivi specifici per il tuo tipo di post personalizzato, come i metadati dei post o forse dal plugin Advanced Custom Fields. Per questi scenari, è possibile includere queste proprietà con l'aggiunta di un frammento come questo per il vostro functions.php di file:

function add_myCustomPostType_fields_url_to_myCustomPostType_request($data, $post, $request) { 
    $_data = $data->data; 

    $customImageProperty = get_field('customImageProperty'); 

    $_data['customImageProperty'] = $customImageProperty['url']; 

    $data->data = $_data; 
    return $data; 
} 
add_filter('rest_prepare_myCustomPostType', 'add_myCustomPostType_fields_url_to_myCustomPostType_request', 10, 3);