/**
*
* add custom post type
*
*/
function my_custom_post_product() {
$labels = array(
'name' => _x( 'Products', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Product' ),
'edit_item' => __( 'Edit Product' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All Products' ),
'view_item' => __( 'View Product' ),
'search_items' => __( 'Search Products' ),
'not_found' => __( 'No products found' ),
'not_found_in_trash' => __( 'No products found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Products'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our products and product specific data',
'public' => true,
//'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'taxonomies' =>array('category','post_tag', 'projects'),
'has_archive' => true,
'show_in_rest' => true
);
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );
/** Code to get json output **/
add_action( 'init', 'add_anuncios_to_json_api', 30 );
function add_anuncios_to_json_api(){
global $wp_post_types;
$wp_post_types['product']->show_in_rest = true;
}
after that, you'll be able to list your posts from mydomain.com/wp-json/wp/v2/posttype_slug
in my case: mydomain.com/wp-json/wp/v2/myposttypename
/**code to add acf field in api**/
add_action( 'rest_api_init', 'add_faculty_type_to_json' );
function add_faculty_type_to_json() {
register_rest_field(
'product', //the post type of your choice
'book_file', //the name for your json element
array(
'get_callback' => 'faculty_return_type', //the function that creates the content
)
);
}
// Return acf field staff_group
function faculty_return_type( $object, $field_name, $request ) {
global $post;
$faculty_type = get_field('book_file', $post->ID);
//print_r($faculty_type);
return $faculty_type['url']; //multiple selections are possible here but in this use case I only want the first one
}
Comments
Post a Comment