top of page

Exposing ACF Fields to WP REST API

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Aug 10, 2020
  • 2 min read

Exposing ACF Fields to REST API is pretty Simple with help of hooks


add_filter('rest_prepare_{post_type_key}', 'acf_to_rest_api', 10, 3);

I have a post type as prod and i need to add following line of code to active theme functions.php

function acf_to_rest_api($response, $post, $request) {
    if (!function_exists('get_fields')) return $response;

    if (isset($post)) {
        $acf = get_fields($post->id);
        $response->data['acf'] = $acf;
    }
    return $response;
}
add_filter('rest_prepare_prod', 'acf_to_rest_api', 10, 3);




I used following code to create ACF 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 '<img src="' . $src[0] . '"  height="150px"/>';
        }
    }
}

I created 3 Fields in ACF Fields for My Post Type



Accessing REST API URL goes this way

http://example.com/wp-json/wp/v2/{post_type_key}

So in our case

http://localhost/wordpress/wp-json/wp/v2/prod

Recent Posts

See All
Hide Upcoming Events

Hiding Upcoming Events can be relatively useful when you are using ACF fields to show Dates for an an Event The Following Snippet is an...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page