wp api – Create a custom post with custom post-metadata with WP API

Question

I’m using WP API to insert a custom post in WordPress. The custom post is “property”.
What I now need to do is assign that post a “metadata” (DB table “wp_postmeta”)

At first I thought It was going to be easy like (I’m using Symfony Http class here):

Http::withBasicAuth($user, $password)->post("{$endpoint}/property", [
    'date' => now(),
    'status'  => 'publish',
    'title' => 'Test',
    //etc...
    'meta' => [
        'custom_metadata' => 1234 
    ]
]);

The custom property post gets created but the meta does not.

I did register my custom post in functions.php like so:

//Add "property" listing capability
function my_custom_post_type_rest_support() {

    global $wp_post_types;
    $post_type_name="property";

    if( isset( $wp_post_types[ $post_type_name ] ) ) {
        $wp_post_types[$post_type_name]->show_in_rest = true;
    }
}
add_action( 'init', 'my_custom_post_type_rest_support', 25 );

And I do get listings and I can filter specific property posts.

I cannot add metadata to the post and I cannot figure out how to do it, and I have a hard time the documentation.

0
Mr.Web 6 months 2022-03-01T13:19:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse