I want to load the content of a modal using the REST API of wordpress. I've added the show_in_rest
param to the custom post type I've registered in my function file, but I'm unable to load it, I will get this error rest_no_route
.
How I can fix this?
Functions:
function staff()
{
$labels = array(
'name' => _x( 'Team', 'post type general name'),
'singular_name' => _x( 'Team', 'post type singular name'),
'menu_name' => _x( 'Team', 'admin menu'),
'name_admin_bar' => _x( 'Team', 'add new on admin bar'),
'add_new' => _x( 'Nuova Persona', 'Aggiungi membro team'),
'add_new_item' => __( 'Name'),
'new_item' => __( 'Aggiungi Persona'),
'edit_item' => __( 'Modifica Persona'),
'view_item' => __( 'Visualizza Team'),
'all_items' => __( 'Visualizza Tutti'),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'search_items' => __( 'Cerca Persona'),
'parent_item_colon' => __( 'Parent:'),
'not_found' => __( 'No member found.'),
'not_found_in_trash' => __( 'No member found in Trash.'),
);
$args = array(
'labels' => $labels,
#'menu_icon' => 'dashicons-star-half',
'description' => __( 'Description.'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','custom-fields')
);
register_post_type( 'staff', $args );
}
add_action('init', 'staff');
JS/AJAX code:
$('.staff-link').on('click', function(e){
e.preventDefault();
var id = $(this).attr('data-id');
var type = $(this).attr('data-type');
console.log(id);
$.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'&id='+id, function(data){
console.log(data);
});
});
I want to load the content of a modal using the REST API of wordpress. I've added the show_in_rest
param to the custom post type I've registered in my function file, but I'm unable to load it, I will get this error rest_no_route
.
How I can fix this?
Functions:
function staff()
{
$labels = array(
'name' => _x( 'Team', 'post type general name'),
'singular_name' => _x( 'Team', 'post type singular name'),
'menu_name' => _x( 'Team', 'admin menu'),
'name_admin_bar' => _x( 'Team', 'add new on admin bar'),
'add_new' => _x( 'Nuova Persona', 'Aggiungi membro team'),
'add_new_item' => __( 'Name'),
'new_item' => __( 'Aggiungi Persona'),
'edit_item' => __( 'Modifica Persona'),
'view_item' => __( 'Visualizza Team'),
'all_items' => __( 'Visualizza Tutti'),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'search_items' => __( 'Cerca Persona'),
'parent_item_colon' => __( 'Parent:'),
'not_found' => __( 'No member found.'),
'not_found_in_trash' => __( 'No member found in Trash.'),
);
$args = array(
'labels' => $labels,
#'menu_icon' => 'dashicons-star-half',
'description' => __( 'Description.'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','custom-fields')
);
register_post_type( 'staff', $args );
}
add_action('init', 'staff');
JS/AJAX code:
$('.staff-link').on('click', function(e){
e.preventDefault();
var id = $(this).attr('data-id');
var type = $(this).attr('data-type');
console.log(id);
$.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'&id='+id, function(data){
console.log(data);
});
});
I have another solution for those who would like to forget the WordPress REST API.
Create your own PHP file where you will return a JSON code of the desired data. Basic structure of the file:
(Note: you need to init your file by including your file in functions.php and the add_action "init")
<?php
$autorisation = isset($_POST['activate']) ? $_POST['activate'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';
if($autorisation !== 'call-staff-post'){
return;
}
if(empty($id)){
// If id empty, return all data
$args = array('post_type' => 'staff', 'posts_per_page' => '-1');
} else {
$args = array('post_type' => 'staff', 'p' => $id);
}
$staff = new WP_Query($args);
$return = array();
while($staff->have_posts()) : $staff->the_post();
$return[] = array(
'title' => get_the_title(),
);
endwhile;
echo json_encode($return);
exit;
?>
Your JS/POST call:
$.post(window.location.href, {
activate: 'call-staff-post',
id: YOUR ID // Need to be integer not a string
}, function(data){
var json = JSON.parse(data);
console.log(json);
});
unregister_post_type('staff')
just before your registration code, and visit the Settings > Permalinks page. Sometimes settings persist until you unregister and re-register, and sometimes they persist until you flush rewrite rules, which just visiting the Permalinks page without making changes does. – WebElaine Commented Nov 22, 2019 at 16:49?id=
param but this isn't working – sialfa Commented Nov 22, 2019 at 16:51wp-json/wp/v2/staff/1234
to have 1 object. – Kaperto Commented Nov 22, 2019 at 17:15