Expose Custom Fields in REST API

Expose Custom Fields in REST API

Code for Custom Post Type

function custom_product_type() {

	$labels = array(
		'name'                  => _x( 'Product', 'Post Type General Name', 'text_domain' ),
		'singular_name'         => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'             => __( 'Product Types', 'text_domain' ),
		'name_admin_bar'        => __( 'Product Type', 'text_domain' ),
		'archives'              => __( 'Product Archives', 'text_domain' ),
		'attributes'            => __( 'Product Attributes', 'text_domain' ),
		'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
		'all_items'             => __( 'All Items', 'text_domain' ),
		'add_new_item'          => __( 'Add New Product', 'text_domain' ),
		'add_new'               => __( 'Add Product', 'text_domain' ),
		'new_item'              => __( 'New Product', 'text_domain' ),
		'edit_item'             => __( 'Edit Product', 'text_domain' ),
		'update_item'           => __( 'Update Product', 'text_domain' ),
		'view_item'             => __( 'View Product', 'text_domain' ),
		'view_items'            => __( 'View Product', 'text_domain' ),
		'search_items'          => __( 'Search Product', 'text_domain' ),
		'not_found'             => __( 'Not found', 'text_domain' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
		'featured_image'        => __( 'Featured Image', 'text_domain' ),
		'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
		'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
		'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
		'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
		'items_list'            => __( 'Items list', 'text_domain' ),
		'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
		'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
	);
	$args = array(
		'label'                 => __( 'Product Type', 'text_domain' ),
		'description'           => __( 'Product Type', 'text_domain' ),
		'labels'                => $labels,
		'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields'),
		'taxonomies'            => array( ),
		'hierarchical'          => false,
		'public'                => true,
		'show_ui'               => true,
		'show_in_menu'          => true,
		'menu_position'         => 5,
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => true,
		'can_export'            => true,
		'has_archive'           => false,
		'exclude_from_search'   => true,
		'publicly_queryable'    => true,
		'rewrite'               => array('slug'=>'product'),
		'capability_type'       => 'page',
		'show_in_rest'          => true,
	);
	register_post_type( 'prod', $args );

}
add_action( 'init', 'custom_product_type', 0 );

add_filter( 'manage_prod_posts_columns', 'set_custom_prod_columns' );
function set_custom_prod_columns($columns) {

    $columns['Price'] = __( 'Price', 'your_text_domain' );
    $columns['Photo'] = __( 'Photo', 'your_text_domain' );

    return $columns;
}

add_action( 'manage_prod_posts_custom_column' , 'custom_prod_columns', 10, 2 );
function custom_prod_columns( $column, $post_id ) {

    switch ( $column ) {
        case 'Price' :
            echo get_post_meta( $post_id , 'price' , true ); 
       
        break; 
        case 'Photo' :    
       	$post_thumbnail_id = get_post_thumbnail_id($post_ID);
        $src = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
        if ($src) {
            echo '' . $src[0]';
        }
    }
}


add_filter('acf/settings/remove_wp_meta_box', '__return_false');




add_action( 'rest_api_init', 'add_custom_fields' );
function add_custom_fields() {
register_rest_field(
'prod', 
'price', //New Field Name in JSON RESPONSEs
array(
    'get_callback'    => 'get_custom_fields', // custom function name 
    'update_callback' => null,
    'schema'          => null,
     )
);
}


function get_custom_fields( $object, $field_name, $request ) {

	$price = get_post_meta( $object['id'], 'price', true );
	return $price;
}

screen76

screen77

Leave a Reply

Your email address will not be published. Required fields are marked *