Giter Site home page Giter Site logo

wp-rest-example's Introduction

REST ACF Example

You can find the following code in the functions.php for the twentytwentyone theme in this codebase.

You will need to add it to the functions.php file in your active theme, but you will be substituting this with your custom post types.

// function to create the custom post type
function create_post_type()
{
	register_post_type(
		'artists',
		array(
			'labels' => array(
				'name' => __('Artists'),
				'singular_name' => __('Artist'),
			),
			'public' => true,
			'has_archive' => true,
			'description' => 'Artists.',
			'map_meta_cap' => true,
			'rewrite' => array('slug' => 'artists'),
			'capability_type' => 'page',
			'hierarchical' => false,
			// This is the important property. This must be true.
			'show_in_rest'    => true,
			'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
			'exclude_from_search' => true,
			'show_in_menu'    => true
		)
	);
}

add_action('init', 'create_post_type');

// This function adds the ACF fields for the post to the response under a key 'acf'
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;
}

// This one specifically targets the post type artists. If you want to add this to multiple post types you would add another one of these for that specific post type.
add_filter('rest_prepare_artists', 'acf_to_rest_api', 10, 3);

Making the REST Call

There are two urls for accessing a custom post type.

Index

/wp-json/wp/v2/{custom-post-type}

This url gives you an array of all posts in the passed in post type. For the example in the code above the url would be /wp-json/wp/v2/artists

Single

/wp-json/wp/v2/{custom-post-type}/{id}

This url gives you the post for the passed in post type and id. For the example in the code above the url would be /wp-json/wp/v2/artists/8

wp-rest-example's People

Contributors

wking-io avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.