• Resolved Mian Shahzad Raza

    (@mianshahzadraza)


    Hi,
    I have just started with REST API and using it for creating posts from frontend. I managed to publish post with Title, Excerpt, Content.
    I want to add a Custom Meta Field value aswell, any example or help is much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Mian Shahzad Raza

    (@mianshahzadraza)

    This is my Ajax Code, all other fields working fine except meta value is not being added in post

    jQuery( document ).ready( function ( $ ) {
        $( '#post-submission-form' ).on( 'submit', function(e) {
            e.preventDefault();
            var title = $( '#post-submission-title' ).val();
            var excerpt = $( '#post-submission-excerpt' ).val();
            var content = $( '#post-submission-content' ).val();
            var status = 'draft';
            var meta = array(
                    'key' => 'video_url_url',
                    'value' =>'abc'
                );
    
            var data = {
                title: title,
                excerpt: excerpt,
                content: content,
                status: status,
                meta: meta
    
            };
    
            $.ajax({
                method: "POST",
                url: POST_SUBMITTER.root + 'wp/v2/posts',
                data: data,
                beforeSend: function ( xhr ) {
                    xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
                },
                success : function( response ) {
                    console.log( response );
                    alert( POST_SUBMITTER.success );
                },
                fail : function( response ) {
                    console.log( response );
                    alert( POST_SUBMITTER.failure );
                }
    
            });
    
        });
    
    } );
    Moderator bcworkz

    (@bcworkz)

    Did you register your meta field to be exposed in REST? This must be done to be able to read or write meta data. See Working with Meta & register_meta near the bottom of the page.

    Once done, I believe you set the value with var meta = {video_url_url: "abc"};
    I’ve yet to set meta values with REST myself, so this is untested and only speculation.

    Thread Starter Mian Shahzad Raza

    (@mianshahzadraza)

    Hi,
    Thank you so much, it worked..

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create Post with Meta Field – WP REST API’ is closed to new replies.