Support » Plugin: WP REST API (WP API) » Custom meta data

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Ryan McCue

    (@rmccue)

    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!

    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.

    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 );

    @rmccue Thanks for this.

    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: http://192.168.205.102:8095/wordpress/?json_route=/posts/36/meta) 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?

    @rmccue : works well for me … thanks

    @ggno37 : what’s your error log saying?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom meta data’ is closed to new replies.