Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix update_callback is not executing in register_rest_field

Discussion in 'Misc WordPress Requests' started by varun naharia, Jan 15, 2018.

  1. varun naharia

    Guest

    update_callback is not executing in register_rest_field, by varun naharia

    I am using below code to get/update custom post meta, get_callback is executing but update_callback is not executing

    add_action( 'rest_api_init', 'create_api_posts_meta_field' );

    function create_api_posts_meta_field() {

    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'experience', 'subtitle', array(
    'get_callback' => 'get_post_meta_for_api',
    'update_callback' => 'update_post_meta_for_exp',
    'schema' => array(
    'description' => 'Subtitle',
    'type' => 'string',
    'context' => array('view','edit')
    )
    )
    );
    }

    function update_post_meta_for_exp($value, $object, $field_name ) {
    $havemetafield = get_post_meta($object['id'], 'experience', false);
    if ($havemetafield) {
    $ret = update_post_meta($object['id'], 'subtitle', $value );
    return true;
    } else {
    $ret = add_post_meta( $object['id'], 'subtitle', $value ,true );
    return true;
    }
    }

    function get_post_meta_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];

    //return the post meta
    return get_post_meta( $post_id )["Subtitle"][0];
    }

    update_callback is not executing in register_rest_field
     
    #1

Share This Page

Monarch Social Sharing Plugin