Viewing 2 reply threads
  • Author
    Posts
    • #8415
      Ico
      Member

      Hi All,
      I searched for an easy way to display meta-box content created by Piklist in the rest_api response.

      For example:

      //Title: Custom meta to show in Rest
       //Post Type: post
      
      piklist( 'field', array(
      	'type'       => 'text',
      	'field'      => 'custom_text',
      	'label'      => 'Custom text demo',
      	//'show_in_rest' => true, //not working :(
      ) );

      I can use register_rest_field to add the meta to the api response ( for example : )

      
      	register_rest_field( 'post', 'cm', array(
      		'get_callback' => function ( $post ) {
      			return get_post_meta( $post['id'], 'custom_text', true );
      		},
      		'schema'       => array(
      			'description' => __( 'Custom piklist text.' ),
      			'type'        => 'string'
      		),
      	) );
      

      and it does the trick, but it would be awesome if just passing 'show_in_rest' => true to the meta params make it automatically show in the response.

    • #10636
      cyclissmo
      Member

      3 years later and this should have been addressed by now. I have to do this same hackery to include piklist fields in my REST endpoints.

    • #10642
      Steve
      Keymaster

      @cyclissmo– I love that you are so used to building with Piklist, that adding fields to the REST API the WordPress way is considered hackery! 😉

Viewing 2 reply threads
  • The topic ‘Add meta to REST API response’ is closed to new replies.