• Hello,

    With the filter json_prepare_post i’m trying to adapt (and remove) some of the data returned when doing a request with these parameters /wp-json/posts?filter[name]=contact&type[]=page. I gave my filter callback a very high priority (999), so as to hook it after all others.

    It works fine as long as the page has no featured image. I dumped each callback for the json_prepare_post filter and just before it is applied in WP_JSON_Posts->prepare_post (1°). Without a featured image this the chronology I get:

    1° before applying filter json_prepare_post for page
    2° add_post_author_data for page
    3° add_thumbnail_data for page
    4° add_term_data for page
    5° CUSTOM json_prepare_post for page

    My own callback (5°) is indeed the last one and I can remove the array elements I want. But when the page has a featured image, my custom callback is only called for the featured image/attachment (6°). Although the json_prepare_post filter is applied twice: once for the page (1°) and once for the attachment (3°).

    1° before applying filter json_prepare_post for page
    2° add_post_author_data for page
    3° before applying filter json_prepare_post for attachment
    4° add_post_author_data for attachment
    5° add_term_data for attachment
    6° CUSTOM json_prepare_post for attachment
    7° add_thumbnail_data for page
    8° add_term_data for page

    If I give my custom callback a priority lower than 10, which is the priority of the default callbacks of the plugin, it is called twice, but too early off course:

    1° before applying filter json_prepare_post for page
    2° CUSTOM json_prepare_post for page
    3° add_post_author_data for page
    4° before applying filter json_prepare_post for attachment
    5° CUSTOM json_prepare_post for attachment
    6° add_post_author_data for attachment
    7° add_term_data for attachment
    8° add_thumbnail_data for page
    9° add_term_data for page

    Any clue?

    Thanks!

    https://wordpress.org/plugins/json-rest-api/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Same.

    I dealt with this by removing some of the default filters:

    function my_api_init($server) {
       // ...custom init code.....
    
        // we don't need all that extra data in the post
        global $wp_json_posts, $wp_json_pages, $wp_json_media, $wp_json_taxonomies;
        remove_filter( 'json_prepare_post',    array( $wp_json_users, 'add_post_author_data' ), 10);
        remove_filter( 'json_prepare_post',    array( $wp_json_media, 'add_thumbnail_data' ), 10);
    }
    add_action( 'wp_json_server_before_serve', 'my_api_init', 11, 1 );

    your mileage may vary…(?)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘json_prepare_post filter, remove/adapt the retrieved data’ is closed to new replies.