Get user all meta data
-
Hello, everyone.
There is a way to get ALL user meta data? Even custom meta keys and values.
I’m using this snippet:
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’,12, 3 );But I think this is only por “/posts”. I’ll need the same but for users.
Thanks in advance!
-
Never mind… use this:
add_filter( ‘json_prepare_user’, ‘add_user_metadata’, 10, 3 );
function add_user_metadata( $user_fields, $user, $context ) {
add_author_meta_field( $user_fields, ‘user_coins’ );
return $user_fields;
}function add_author_meta_field( &$user_fields, $field ) {
if ( $meta = get_the_author_meta( $field, $user_fields[‘ID’] ) ) {
$user_fields[ $field ] = $meta;
}
}and “add_author_meta_field” for every meta key you need.
Hope that helps someone else!
BTW… how can I make a POST request to a custom meta key?
mydomain.com/wp-json/users/1?data[my_meta_key]=2500
is not working… any hint for that?
EDIT: I also try http://wp-api.org/#posts_create-meta-for-a-post without any luck. Is a lack of info in that with the endpoint
Should be something like this: my-domain.com/wp-json/posts/1223/meta?data[my_meta_key]=2500
of what???
PS: Every other “native” meta key is working with POST request and I can edit it.
If someone need it:
user Basic Auth (or any other Auth), then:
my-domain.com/wp-json/posts/IDofPost/meta/MetaID
the use value pair to upload the value of it. Normally, having the ID of the Meta, you only need to pass: value=Example
if you want to use full endpoint is:
my-domain.com/wp-json/posts/IDofPost/meta/MetaID?data[value]=Example
PS: Change IDofPost and MetaID for the values you need. Check your meta response to know which one you need to edit.
The topic ‘Get user all meta data’ is closed to new replies.