0

I return data using the post id through AJAX. Now, what is the best way to "merge" the image thumbnail ( get_the_post_thumbnail( $id, 'medium' ) ) into the data returned from the server?

I need this as a single JSON object for passing it to a template (all this is working fine, just need the image as well).

The simple idea:

$post = get_post( $post_id );

$post['image'] = get_the_post_thumbnail( $post_id, 'medium' );

echo json_encode( $post );
  • Hi. What do you mean by merge? can you please post your current code? – Jack Johansson Jun 10 '17 at 9:19
  • I'm simply trying to add the post image thumbnail to the post data output - to get all I need on the frontend in one single call to the server. Like: $post = get_post( $post_id ); $post['image'] = get_the_post_thumbnail( $the_post_id, 'medium' ); echo json_encode( $post ); – blaasvaer Jun 10 '17 at 9:26
  • I know. But if you want us to write an accurate answer, you need to provide us with the code you are currently using. Your idea works, but it seems you want something better. That's why i'm asking you to add your code so i can modify. – Jack Johansson Jun 10 '17 at 9:32
  • No, my »idea« does not work. I'd be more that thrilled if it did. I always try to make things as simple as possible, but somehow the inner »workings« of Wordpress seem to be giving me a hard time. In this case $post goes from being and Array to an Object when pushing parameters. I've also tried array_push ... no luck. – blaasvaer Jun 10 '17 at 9:55
  • This is just one of the many things it throws at me: Fail: Object { readyState: 4, getResponseHeader: getResponseHeader(), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), catch: catch(), 9 more… } – blaasvaer Jun 10 '17 at 9:57
0

The reason that your idea doesn't work is, get_post returns a post object, not an array (if not set manually).

This is a quote from WordPress code reference:

When $output is OBJECT, a WP_Post instance is returned.

Which is the default output type for get_post(). If you need to implement an array of data and then encode it using JSON, do this:

// Let's retrieve the post object
$post = get_post( $post_id );
// Set up the post data to use in our code
setup_postdata($post);
// Create an empty array for the data
$json_input = array();
$json_input['image'] = get_the_post_thumbnail( $post->ID, 'medium' );
$json_input['excerpt'] = get_the_excerpt();
$json_input['content'] = get_the_content();
$json_input['title'] = get_the_title();
// Now, we should reset the post data to avoid conflict
wp_reset_postdata();
// Our data is ready to be encoded, just, DO IT!
$json = json_encode($json_input);

However, you can set the output of get_post() to return an array, by changing the second argument:

get_post( $post_id, ARRAY_A );
|improve this answer|||||
  • Ok, found the solution and posted on top of your answer ... sorry. – blaasvaer Jun 10 '17 at 10:12
  • @blaasvaer No problem, Glad it help you out. – Jack Johansson Jun 10 '17 at 10:13
  • @kaiser Well it was simply an answer to my own question. Why was is deleted? – blaasvaer Jun 10 '17 at 13:25
  • @blaasvaer If the answer is duplicate, it will be removed by the moderator, because it already exists and is marked as accepted. You can read the guidelines here. – Jack Johansson Jun 10 '17 at 13:30
  • Yeah, I know. But if you look at the amount of text used to answer, and my extract, I bet another one needing the answer would get it quicker. And ideally with at link to the codex. Just saying. – blaasvaer Jun 11 '17 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.