How to include META fields in Wordpress API Post?
First of all, you need to set the 'show_in_rest'
property to true
and 'supports
' property should include 'custom-fields'
when you are registering a new post type.You need to support 'custom-fields' if you want to include the meta fields.
Note that for meta fields registered on custom post types, the post type must have custom-fields support. Otherwise, the meta fields will not appear in the REST API. https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
function cptui_register_my_cpts() { /** * Post Type: Experiences. */ $labels = array( "name" => __( "Experiences", "twentynineteen" ), "singular_name" => __( "Experience", "twentynineteen" ), ); $args = array( "label" => __( "Experiences", "twentynineteen" ), "labels" => $labels, "description" => "", "public" => true, "publicly_queryable" => true, "show_ui" => true, "delete_with_user" => false, "show_in_rest" => true, "rest_base" => "", "rest_controller_class" => "WP_REST_Posts_Controller", "has_archive" => false, "show_in_menu" => true, "show_in_nav_menus" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => false, "rewrite" => array( "slug" => "experience", "with_front" => true ), "query_var" => true, "supports" => array( "title", "editor", "thumbnail", "custom-fields" ), ); register_post_type( "experience", $args );}add_action( 'init', 'cptui_register_my_cpts' );
Now, you need to register the meta fields using the register_meta()
.
add_action( 'rest_api_init', 'register_experience_meta_fields');function register_experience_meta_fields(){ register_meta( 'post', 'location', array( 'type' => 'string', 'description' => 'event location', 'single' => true, 'show_in_rest' => true )); register_meta( 'post', 'date', array( 'type' => 'string', 'description' => 'event location', 'single' => true, 'show_in_rest' => true )); register_meta( 'post', 'event_url', array( 'type' => 'string', 'description' => 'event location', 'single' => true, 'show_in_rest' => true ));}
Note: The meta fields must be unique. Prefix your fields in ACF to make it unique.
JSON doesn't use single quotes to wrap a string. It uses double quotes. You are sending invalid JSON.
Now, if you want to create a experince post type.Use a JSON linter to validate your json. https://jsonlint.com/
Make a POST request to http://paathsala-plugin.test/wp-json/wp/v2/experience, with the fields
{ "title": "test", "content": "testingfrompython", "status": "draft", "author": 1, "meta": { "location": "NYC", "date": "never", "event_url": "http: //google.com" }, "featured_media": 1221}
WordPress doesn't allow you create resource directly. You need to authenticate your REST request. I am using the Basic Auth for authenticating WordPress REST API. You need to install a plugin. Grab it from here: https://github.com/WP-API/Basic-Auth
I have tested the following python code.
import base64import jsonimport requests;# Data to be senddata = { "title": "test", "content": "testingfrompython", "status": "draft", "author": 1, "meta": { "location": "NYC", "date": "never", "event_url": "http: //google.com" }, "featured_media": 1221}# I am using basic auth plugin to for WP API authenticaitonusername = 'admin'password = 'blood9807'# Encode the username and password using base64creds = base64.b64encode(username + ':' + password)# Create headers to be sendheaders = { 'Authorization': 'Basic ' + creds, 'Content-type': 'application/json', 'Accept': 'text/plain'}# Convert the python dictionary to JSONdata_json = json.dumps(data)# Create a postr = requests.post('http://paathsala-plugin.test/wp-json/wp/v2/experience', data = data_json, headers = headers )print(r)
@Peter Foti can you try below function for add meta value in your API.
add_post_meta( <value>, <name>, $meta_value ,true );
For Reference see this Link