• Resolved mompracem

    (@mompracem)


    Hello

    I’m building a WordPress frontend entirely based on the WP API output. That means, no php involved, but purely javascript reading from the json.

    I have met one difficulty though. There’s a lot of stuff I don’t need in the json in certain views. For example I’d like to download the entire website data in one single paged json. That’d allow me to do things like filtering posts, data and so on, directly from user’s machine.

    However, to optimize this, I need to filter a lot of contents from the original json object. Suppose I query the wp-json.php/posts/ route… I have a lot of post data which I don’t need. Content, some post_meta, feature image data, taxonomies… Suppose I’d want only few things like IDs, title, excerpt… How to remove all the rest?

    I tried ‘json_prepare_post’ filter, but I wasn’t able to conditionally alter content, say, on usual WordPress query conditionals (is_singular(), is_archive()…). If I attempt to do this, it will filter the json also in posts/id route (the singular view, which I need to be complete) I haven’t figured out how to do this. I don’t think it should be necessary to write an entire new route…

    Any suggestions?

    https://www.ads-software.com/plugins/json-rest-api/

Viewing 9 replies - 1 through 9 (of 9 total)
  • You can try filtering the prepare posts method in the posts class:

    return apply_filters( ‘json_prepare_post’, $_post, $post, $context );

    If the post class is just too off base for what you need specifically, you can always create a new class like I mentioned here (last few posts):

    https://www.ads-software.com/support/topic/registration-and-examples?replies=5

    You can also create your own server class (hook available in plugin.php) and manipulate to produce desired results (copy the default server class, rename, then hook):

    $wp_json_server_class = apply_filters(‘wp_json_server_class’, ‘WP_JSON_Server’);

    You can also directly call the response method in the server class (via a custom method in a custom/extended class):
    public function prepare_response($data) {}

    Thread Starter mompracem

    (@mompracem)

    Hi Dunar,

    yes I tried alread json_prepare_post… it would suffice if it were not for the fact that I can’t use conditionals such as is_singular, if this were a normal wp query… For example, for the archives, I don’t need custom metas or a lot of stuff, content, etc. If I could create a filter with something as:

    if ( !is_singular() ) {
      unset([$_post]['content']);
    }

    then I’d have what I want – I’m looking for something similar. It doesn’t work because each post in the object is looped individually, so it’s always singular – conditionals such as if ( get_post_type == ‘attachment’ ) will work and I can filter content of the json object according to post type for example, or other individual post parameters… What I need to do is alter the json object according to route… individual post or root /posts/ – I don’t need anything else ??

    thanks

    Yeah, I don’t think you are completely familiar with the methods you are trying to use or the api you are using. There is nothing you can’t do with the api that is available in wordpress. It is not complete and has it’s limitations as is, but it can be extended to do whatever you want. I’ll sign off this thread. Good luck.

    Plugin Author rmccue

    (@rmccue)

    Queries aren’t handled the same in WP API, so what you want to do is look at the value of $context and base your result off that.

    Thread Starter mompracem

    (@mompracem)

    Hi Ryan, thank you…

    but how do I access $context from ‘json_prepare_post’ filter? I tried, doesn’t look like it’s passed in there, php throws an error – what values $context can have?

    thanks again

    Plugin Author rmccue

    (@rmccue)

    but how do I access $context from ‘json_prepare_post’ filter? I tried, doesn’t look like it’s passed in there, php throws an error – what values $context can have?

    You’ll need to make sure you get all the parameters by passing 3 for the parameter-count value to add_filter:

    add_filter( 'json_prepare_post', 'myfunc', 10, 3 );

    With this, you should get all of the parameter values correctly passed in.

    Thread Starter mompracem

    (@mompracem)

    ok I did

    but eventually I managed to achieve to do what I wanted by conditionally adding different filters depending on where the query is happening (front page, a post type archive, a singular post…)

    what I wanted is to produce different api results based on the WordPress view. For example, if the user was querying the front page, I wanted a very compact json object.

    so I added my filters a bit like this:

    if ( is_front_page() ) {
        add_filter( 'json_prepare_post', 'my_front_page_filter', 10, 3 );
    } else {
        add_filter( 'json_prepare_post', 'my_json_filter', 10, 3 );
    }

    so far this is the only way I found to achieve what I wanted…

    it’s probably not the correct way, but I didn’t understand how to do that by using $context or within being in the same filter function

    @mompracem: How does your my_json_filter function look like?

    I am looking at a way to filter unused items from the json.

    I tried this
    add_filter( ‘json_prepare_post’, function ($data, $post, $context) {
    unset($data[‘author’]);
    return $data;
    }, 10, 3 );
    but instead of delete [‘author’] from my array it moved it to another position in the json.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Filter or change the contents of the json object conditionally’ is closed to new replies.