How to include META fields in Wordpress API Post?
Asked Answered
I

2

13

I am using Wordpress version 5.2.1 which means I am not using a plugin for the WP API. I have working code which creates a new post (using a custom post type) on my WP site, all good there. The problem is that i've created custom fields using ACF, but they're not appearing in the newly created post. The following is an example of what I am sending to WP via: /wp-json/wp/v2/experience.

Note: experience is my custom post type.

{'title': 'test', 'content': 'testing from python', 'status': 'draft', 'author': 1, 'meta': {'location': 'NYC', 'date': 'never', 'event_url': 'http://google.com'}, 'featured_media': 1221}

This creates a post, but the fields inside of meta are completely ignored. My goal is to have the fields I specify in meta be included in my newly created post .

I have tried the solutions listed at the following URLs and nothing has worked.

https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51

Wordpress Rest API - Custom Fields

https://jeffreyeverhart.com/2017/06/14/adding-custom-fields-wordpress-json-api/

What am I missing?

Irreducible answered 5/6, 2019 at 12:31 Comment(1)
Have you seen the fields being ignored in the dashboard (ACF not showing any custom fields), or did you check the wp_postmeta table as well? It might be that ACF has problems displaying it rather than WP API storing the meta values.Emersen
R
27

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.

enter image description here

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
}

enter image description here

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 base64
import json
import requests;

# Data to be send
data = {
    "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 authenticaiton
username = 'admin'
password = 'blood9807'

# Encode the username and password using base64
creds = base64.b64encode(username + ':' + password)

# Create headers to be send
headers = {
    'Authorization': 'Basic ' + creds,
    'Content-type': 'application/json', 
    'Accept': 'text/plain'
}

# Convert the python dictionary to JSON
data_json = json.dumps(data)

# Create a post
r = requests.post('http://paathsala-plugin.test/wp-json/wp/v2/experience', data = data_json, headers = headers )

print(r)
Rearmost answered 8/6, 2019 at 18:10 Comment(6)
I am using CPTUI and while I did not previously have supports -> custom-fields, i've added it. After adding that however I am getting the following error when making a request (after having copied the registration code you posted). {'code': 'rest_invalid_param', 'message': 'Invalid parameter(s): meta', 'data': {'status': 400, 'params': {'meta': 'Invalid parameter.'}}}Irreducible
Re: Invalid JSON, I am using the Python requests library so it is being formatted correctly. It formats differently when its being printed to the console.Irreducible
@PeterFoti I have updated the answer with working python code. Check it out.Rearmost
@SagarBahadurTamang When I do this I get this error: { "code": "rest_cannot_update", "message": "Sorry, you are not allowed to edit the email custom field.", "data": { "key": "email", "status": 403 } }Punkie
@Punkie Are you using basic auth to authenticate yourself?Rearmost
it was a role issue. The login was set to a subscriber .Punkie
F
1

@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

Flummery answered 8/6, 2019 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.