• Kingdom Creation

    (@kingdomcreation)


    Plugin looks fantastic Ryan!

    Just a quick question…check this out
    I included a preview of the results im getting, would it be possible to use the API to format the post_meta.post_object to become like the rest of the api, ex: (instead of ‘post_author‘ it would be ‘author‘ etc… )

    Also i notice the author data isnt completed like in the result you give, so my question really is

    1. Is there a way to ‘prepare_post’ for data outside the API server. (some kind of local php client ??? )

    2. What do you suggest

    {
        ID: 1,
        title: "Test",
        status: "publish",
        type: "page",
        author: {
            ...
       },
       content: "<h1>Lorem ipsum dolor sit amet ... ",
       parent: 0,
       link: "https://domain.top/test",
       date: "2014-11-25T14:50:20+00:00",
       modified: "2014-11-25T21:20:23+00:00",
       format: "standard",
       slug: "test",
       ...
       post_meta: {
            post_object: {
                ID: 1,
                post_author: "1",
                post_date: "2014-11-01 04:33:09",
                post_date_gmt: "2014-11-01 04:33:09",
                post_content: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!",
                post_title: "Hello world!",
                post_excerpt: "",
                post_status: "publish",
                comment_status: "open",
                ping_status: "open",
                post_password: "",
                post_name: "hello-world",
                to_ping: "",
                pinged: "",
                post_modified: "2014-11-01 04:33:09",
                post_modified_gmt: "2014-11-01 04:33:09",
                post_content_filtered: "",
                post_parent: 0,
                guid: "https://domain.top/?p=1",
                menu_order: 0,
                post_type: "post",
                post_mime_type: "",
                comment_count: "1",
                filter: "raw"
            },
            user: {
                ID: 1,
                user_firstname: "",
                user_lastname: "",
                nickname: "...",
                user_nicename: "...",
                display_name: "...",
                user_email: "...",
                user_url: "",
                user_registered: "2014-11-01 04:33:09",
                user_description: "",
                user_avatar: "<img alt='' src='...' class='avatar avatar-96 photo' height='96' width='96' />"
            },
        },
    
        featured_image: null,
        terms: [ ]
    }

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Kingdom Creation

    (@kingdomcreation)

    rjvansant

    (@rjvansant)

    just posting my response (https://www.ads-software.com/support/topic/a-way-to-utilize-the-json-rest-api-libraries-to-generate-embedded-post-json) to my question here as well, thanks for linking the similar questions @kindom creation

    @kingdom creation, sorry i meant to post an update, but i got caught up in meeting a deadline. some of this was due to me still learning the WordPress Way, where i came from a MVC Framework background, but basically you can refer to the global in your function like so:

    global $wp_json_posts;

    and utilize the get_post() method, passing in post_id, and ‘child’ to indicate recursive lookups or embedding, example below:

    $wp_json_posts->get_post( $post_id, ‘child’ );

    the one caveat is make sure your method that will use this has access to the children post_ids, which it looks like it does based on your pasted code.

    happy coding,
    ~rob

    Thread Starter Kingdom Creation

    (@kingdomcreation)

    Hi,

    It seems that the variable global $wp_json_posts; is undefined in my context. I believe this may be somehow related to the fact that i’m using it outside the server process upon save_post.

    Currently i’m converting a large array that contains ‘WP_Post’ and authors and all kinds of WordPress data objects.

    I considered parsing the properties and renaming the indexes kinda like the api does in ‘prepare_post’ but it just seems so redundant.

    What i do then is save the data in a post_meta. ( and i would like it to be saved formatted the same way the api does). In another context i then pull the data back when that api summoned, I use the fillowing code.

    functions.php

    add_filter( 'json_prepare_post', function ($data, $post, $context) {
    	$data['post_meta'] = get_post_meta( $post['ID'],'_data', true );
    	return $data;
    }, 10, 3 );

    But as you can see above in my original question, the json from the api is formatted by the api to look nice and be useful (author instead of post_author, with meaningful data inside not just an index) but the data i added ‘post_meta‘ contains some data that isn’t nicely formatted.

    All i would need really would be the create an instance of the WP_JSON_Posts but the authors @ryanmccue made it so i think it shoulnt be instantiated outside of it’s server component.

    Although the function ‘get_post’ (witch i think is the one @rjvansant suggested would be super useful. and is basically what i need, it’s right there just i can use it in the admin)

    So i considered, what if i use PHP to query the API and get the formatted data that way, i tried curl, i tried file_get_contents etc. even i tried to get @ryanmccue php-client in my theme….

    1. . Is there a way to reuse the api formatting blocs used ‘in’ the api outside the api.

    The other hook inside my function is

    add_action('save_post', 'update_data',20,1);
    
    function update_data($post_id){
      <em> global $wp_json_posts; // @rjvansant</em>
       ...
    }

    Thanks! @rjvansant

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need to 'prepare_post' data’ is closed to new replies.