[resolved] Custom meta data (7 posts)

  1. chirinyu
    Posted 1 year ago #


    got to say I'm really excited about the version 1.0!

    I'm having some problems after updating to 1.0, when I try to get posts or a single post for example wp-json/posts/ or wp-json/posts/11/ there's no post_meta anymore, there used to be, I have lots of Advanced Custom Fields. Also, wp-json/posts/11/meta (get) I get this as a result:
    [{"code":"json_cannot_edit","message":"Sorry, you cannot edit this post"}]
    Everything else works perfectly, except for the missing custom meta data.


  2. Ryan McCue
    Plugin Author

    Posted 1 year ago #

    Hey chirinyu,

    Due to security/privacy concerns, custom meta is now only available to authenticated users. You'll need to authenticate as a user, then pass ?context=edit with the request.

    If you need this data when unauthenticated, you can make a quick plugin to filter json_prepare_post and add in the fields you'd like:

    add_filter( 'json_prepare_post', function ($data, $post, $context) {
    	$data['myextradata'] = array(
    		'somekey' => get_post_meta( $post['ID'], 'abcdef', true ),
    	return $data;
    }, 10, 3 );

    This data would then be available as the myextradata key in the post. :)

    Hope that helps!

  3. Dave van Hoorn
    Posted 1 year ago #

    I can't seem to figure out how this works. Isn't it possible to get all the custom fields data if I loop over a certain amount of posts?

    My URL endpoint is '/posts?type=recepten', and I would like to see the post_meta field so I can get the values of certain custom fields and display them.

    Thanks Ryan for creating this great plugin, but please help me out to get it working the way I'd like to again.

  4. aryanduntley
    Posted 1 year ago #

    Thankfully, author has added a good number of hooks in order to allow customization of action, extension and results. I found that I was unable to retrieve a num post count when querying a post type. Because I plan to interface with a mobile app, I want to allow for them to correctly display pagination. In order to do that, they will need to know either how many pages can be made regulated by posts per or how many posts total so they can calculate it themselves. I hooked into post type data to add that information to the result array:

    function send_num_pages_too($data, $type){
    	$added = array();
    	//"publish"]=> string(4) "8417" ["future"]=> int(0) ["draft"]=> int(0) ["pending"]=> int(0) ["private"]=> int(0) ["trash"]=> int(0) ["auto-draft"]=> int(0) ["inherit"]=> int(0)
    	$pcount = wp_count_posts($data['slug'], 'readable');
    	$added['num_posts'] = (int)$pcount->publish;
    	$added['num_private_posts'] = (int)$pcount->private;
    	return array_merge($data, $added);
    add_filter( 'json_post_type_data', 'send_num_pages_too', 10, 2);

    In your guys' situations, author indicates that with json_prepare_post hook, you can customize the output of your query. He also states that if you pass the param context=post you get all the meta. Here's the code so you can see that you are getting that (the $post_fields_raw['post_meta'] returns almost all meta. if you have not requested raw data, you will not get back serialized values, otherwise, you will)

    if ( 'edit' === $context ) {
    			if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) {
    				if ( is_wp_error( $post_fields_raw['post_meta'] ) ) {
    					return $post_fields_raw['post_meta'];
    				$_post = array_merge( $_post, $post_fields_raw );
    			} else {
    				return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) );

    Or if you hook into the json prepare post filter, you can do something like this:

    function playWithMeta($postray, $postdat, $context){
         $morestuff =  get_post_meta($postdat['ID']);
         //don't do this, you will probably be duplicating a bunch of stuff
         return array_merge($postray, $morestuff);
    add_filter( 'json_prepare_post', 'playWithMeta',10, 3 );
  5. senica@gmail.com
    Posted 7 months ago #

    @rmccue Thanks for this.

  6. GGno37
    Posted 6 months ago #

    Hi, i'm using the 1.2.1 version of wp rest api and i have the same problem, http 403 while getting meta of a post.
    But if i try on my local wamp installation (eg: http://localhost/wordpress/?json_route=/posts/143/meta) it works perfectly, if i try to query a remote server (eg: i get the famous 403.
    The httpd.con files are identical, i'm using chrome advanced rest client extension to run the querys on a anonymous navigation window, to get rid of coockies.
    Im using basic autentication.

    What's wrong?

  7. kaushalbizwork
    Posted 6 months ago #

    @rmccue : works well for me ... thanks

    @GGno37 : what's your error log saying?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic