Support » Plugins and Hacks » Retrieve media info for a post

Retrieve media info for a post

  • Hi Guys,

    I’m trying to use this api in an iOS app to retrieve a list of post/categories and images for the posts. When I retrieve posts their is no info as to whether there’s any image associated with this post. I can grab a list of media and those associated with a post will list the post content in the parent element.

    This seems very inefficient to me as it requires two separate calls to get media for a post, would it be possible to list the media entry if there is one in the posts meta data? Mobile apps need to minimise the network traffic as much as possible.

    I’ve also spotted what appears to be a bug, if I grab a list of media items and extract the posts from that the meta link to get replies (comments) will be something like xxx/wp-json.php/media/4481/comments this fails – replacing media with posts enables them to be retrieved ok.



Viewing 7 replies - 1 through 7 (of 7 total)
  • Actually have the same question. Has this ever been answered anywhere?

    This relates to

    You can do something like this (untested):

    function playWithMeta($postray, $postdat, $context){
         $imstff = wp_get_attachment_image_src( get_post_thumbnail_id($postdat['ID']), 'thumbnail');
         $morestuff = array("imsrc"=>$imstff[0], "imwid"=>$imstff[1], "imhit"=>$imstff[2]);
         return array_merge($postray, $morestuff);
    add_filter( 'json_prepare_post', 'playWithMeta',10, 3 );

    Just tested and works fine, but image data is coming through by default. Maybe when creating the posts, you are not correctly assigning thumbnail url?

    I’m not creating posts, but retrieve them (GET vs POST). In a GET request for /get/post/id the post_thumbnail is in the object, but not all the other attachments (images, audio, video, etc) in that post. And I can’t seem to find how to retrieve attachment objects (via /get/media ?) for a specific post ID.
    This is also what mslight wants as I read correctly: just let us retrieve the attachment(s) objects in the same object request for /get/post/id (or any other /get/post* request), instead of just only post_thumbnail.

    The previous code was for getting. It is hooking the prepare post method. You kind of come off as whining to me (“this is what [the other guy] wants too” and “just let us…”), maybe I’m just in a bad mood, but I don’t care if I sound mean, so, if you can’t figure out how to get what you are looking for from the gimme code I gave you above, you probably shouldn’t be using this plugin and should instead hire a developer.

    Still, here’s more spoon feeding. You are provided, here, with two ways of retrieving the post’s associated attachment data; get_children and get_posts. Use only one. This is wrapped in the exact same function and hook as previous, just changed the code inside to get the things you needed. Take it and learn from it instead of complaining when it doesn’t exactly fit your needs. I’m trying to gives examples of how to fish, not shove fish down your throat every time you whine about being hungry.

    function playWithMeta($postray, $postdat, $context){
    	 $args = array(
    		'post_parent' => $postdat['ID'],
    		'post_type'   => 'attachment',
    		'posts_per_page' => -1,
    		'post_status' => 'any'
    	$chilattaches = get_children($args);
    		$postray = array_merge($postray, (array)$chilattaches);
    	$attachments = get_posts( array(
    		'post_type' => 'attachment',
    		'posts_per_page' => -1,
    		'post_parent' => $postdat['ID'],
    		//'exclude'     =>
    	) );
    		foreach ( $attachments as $attachment ) {
    			$class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
    			$thumbimg = wp_get_attachment_link($attachment->ID, 'thumbnail-size', true);
    			echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
    		$postray = array_merge($postray, (array)$attachments);
    	return $postray;
    add_filter( 'json_prepare_post', 'playWithMeta' ,10, 3 );

    Thanks for the code. You completely misinterpreted my tone, but that’s fine. It’s the internet and I’m not a native English speaker, so it goes with the territory. But a well meant and sincere thank you for your elaborate explanation _O_ This code snippet does help me a lot (‘spoon fed’ or not).

    Ok, your welcome. Apologies for the misinterpretation. Glad to help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Retrieve media info for a post’ is closed to new replies.