Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello prajaktadhanke! To get the content of an specific user, you must know the user ID. To get all users and users ID, use this endpoint: your-url-goes-here.com/wp-json/users

    One you know the user ID, simple use this:

    your-url-goes-here.com/wp-json/posts?filter[author]=1

    ID: 1 normally is the “admin”. If “co-author josh” (for example) is ID: 2… then, use filter[author]=2 at the end of your URL.

    Ones you have that, install “WP REST API Filter Fields” plugin and add this endpoint: &fields=ID,title,content,featured_image,date

    That will only shows you those values with a clean small json response.

    PS: Please, replace “your-url-goes-here.com” with your actual URL

    Thread Starter prajaktadhanke

    (@prajaktadhanke)

    Hi mkiisoft,

    Thanks for the reply.
    I used these plugin and its working so fine.but its giving meta data of the author & other.

    Then, use this next snippet over fuctions.php (of your theme) or download the plugin “Code Snippets” (save and activate to make it work):

    function remove_extra_data( $data, $post, $context ) {
    // We only want to modify the ‘view’ context, for reading posts
    if ( $context !== ‘view’ || is_wp_error( $data ) ) {
    return $data;
    }

    // Here, we unset any data we don’t want to see on the front end:

    unset( $data[‘meta’] );

    // To unset tree nested specific data, you need to declare the
    // “parent” key and then the actual key, for example:
    // unset( $data[‘author’][‘description’] );
    // unset( $data[‘author’][‘URL’] );
    // that’ll only unset description and URL inside author.
    // continue unsetting whatever other fields you want

    return $data;
    }

    add_filter( ‘json_prepare_post’, ‘remove_extra_data’, 12, 3 );

    Tell me if that works! πŸ™‚

    PS: If you want to build an app… meta info is a good for you. You can actually check it from the json response and not saving that data locally on you app. If the meta URL changes, you’ll have to update the entire app.

    Thread Starter prajaktadhanke

    (@prajaktadhanke)

    Thanks mkkisoft

    Its working smoothly.

    Hey mkiisoft,

    This was great help, but I seem to have an issue with one thing I hope you can help with.

    When I try to unset nested items, it does not work for me. I used your example exactly as it is for the Author but the fields still remain when I view the JSON code.

    This is what I have. The first fields do hide, but when I try to add the nested fields, it does not work. Do you know of a reason why?

    function remove_extra_data( $data, $post, $context ) {
    
      if ( $context !== 'view' || is_wp_error( $data ) ) {
        return $data;
      }
    
     unset( $data['date_gmt']);
      unset( $data['modified_tz']);
      unset( $data['modified_gmt']);
      unset( $data['meta']);
      unset( $data['author']['first_name']);
      unset( $data['author']['description'] );
    
     return $data;
    }
    
    add_filter( 'json_prepare_post', 'remove_extra_data', 10, 3 );

    Thanks

    Damien

    Hello @damienoneill2001

    You need to change the level permission for that… that would be the “10” at the end. Try using 12, 2, 4 or similar. 2 works for me in some cases.

    If possible, only the examples shown in internet is:

    qod_remove_extra_data function ($ data, $ post, $ context) {
            // We only want to modify the 'view' context, for reading posts
            if ($ context! == 'view' || is_wp_error ($ data)) {
                return $ data;
            }
            // Here, we unset any data we do not want to see on the front end:
            unset ($data ['author']);
            unset ($data ['status']);
            // Continue unsetting whatever other fields you want return $ data;
        }
        add_filter ('json_prepare_post' 'qod remove extra_data', 12, 3);

    and right is:

    qod_remove_extra_data function ($ data, $ post, $ context) {
            // We only want to modify the 'view' context, for reading posts
            if ($ context! == 'view' || is_wp_error ($ data)) {
                 unset ( $data->data ['excerpt']); //Example
                 unset ($data->data ['content']); //Example
                 unset ($data->data ['name field to remove'])
                 //or
                 unset ($data->data ['name field to remove'] ['name subfield if you only want to delete the sub-field of field' ])
                 return $data;
             }
        }
        add_filter ('rest_prepare_post' 'qod_remove_extra_data', 12, 3);

    IMPORTANT, Is:

    add_filter ('rest_prepare_post' 'qod_remove_extra_data', 12, 3);

    Not:

    add_filter ('json_prepare_post' 'qod remove extra_data', 12, 3); //WRONG

    If is Custom Post Type:

    add_filter ('rest_prepare_{$post_type}' 'qod_remove_extra_data', 12, 3);

    EXAMPLE: Name post type = product;

    add_filter ('rest_prepare_product' 'qod_remove_extra_data', 12, 3);

    With this code can remove the fields that you want the JSON. By using rest_prepare} _ {$ post_type decide that you eliminated every post_type fields, thus only affected the post_type you want and not all.

    carfis

    (@carfis)

    Hi @cristophergv
    Thank you for your explanation. Do you know if is possible to remove the _link from the json?

    I use this code:

    function cflex_remove_json_fields( $data, $post, $context ) {
    
        unset($data->data['date']);
        unset($data->data['date_gmt']);
        unset($data->data['guid']);
        unset($data->data['modified']);
        unset($data->data['modified_gmt']);
        unset($data->data['type']);
        unset($data->data['_links']);
    
        return $data;
    
    }
    add_filter( 'rest_prepare_product', 'cflex_remove_json_fields', 12, 3 );

    but I can’t remove the data[‘_links’]

    Best regards
    Carlos

    @carfis, I had the same question as you. Searching the API, I came across this: http://v2.wp-api.org/extending/linking/ which says that _Links propery is “prefixed with an _ because it’s a “meta” property, not part of the data”. So I assume that it is not possible to simply unset it like the rest of the fields.

    After a lot of searching I found that you can remove some links under _link by doing for example:

    $data->remove_link('collection');
    This is only a half measure, though, because you can apply it to certain links. For example, it doesn’t work with anything that begins with wp: (e.g. wp:terms).

    Eventually, I ended up with a totally different approach: Instead of unsetting the unnecessary fields, I returned only those that I needed, using:

    add_filter( 'rest_prepare_post', 'my_custom_json_fields', 12, 3 );
    function my_custom_json_fields( $data, $post, $context ) {
    	return [
    		'title'    => $data->data['title']['rendered'],
    		'link'     => $data->data['link']
    	];
    }

    That way the json contains only the fields that I want – in the above example, only the title and the url.

    @giorgos Sarigiannidis,
    I get the answer, why the unset for _links isn’t possible.

    Links aren’t handled the same way as the rest of the data, and it’s not really possible to remove them.

    Hi Guys,

    Is there any way to get response for custom post meta fields with this rest api. I’m new to word press so i need step by step how to get those postmeta table fields with api responses.And also i need to know which file need above mentioned example code.

    add_filter( 'rest_prepare_post', 'my_custom_json_fields', 12, 3 );
    function my_custom_json_fields( $data, $post, $context ) {
    	return [
    		'title'    => $data->data['title']['rendered'],
    		'link'     => $data->data['link']
    	];
    }
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to write custom(my own) api in WP REST API Plugin?’ is closed to new replies.