WordPress.org

Ready to get started?Download WordPress

Forums

JSON REST API (WP API)
Retrieve media info for a post (8 posts)

  1. mslight
    Member
    Posted 8 months ago #

    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.

    Cheers,
    Mark

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

  2. JeanPaulH
    Member
    Posted 3 months ago #

    Actually have the same question. Has this ever been answered anywhere?

  3. dunar21
    Member
    Posted 2 months ago #

    This relates to
    http://wordpress.org/support/topic/custom-meta-data-2?replies=4.

    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 );
  4. dunar21
    Member
    Posted 2 months ago #

    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?

  5. JeanPaulH
    Member
    Posted 2 months ago #

    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.

  6. dunar21
    Member
    Posted 2 months ago #

    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);
    	if($chilattaches){
    		$postray = array_merge($postray, (array)$chilattaches);
    	}
    	$attachments = get_posts( array(
    		'post_type' => 'attachment',
    		'posts_per_page' => -1,
    		'post_parent' => $postdat['ID'],
    		//'exclude'     =>
    	) );
    
    	if($attachments){
    		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 );
  7. JeanPaulH
    Member
    Posted 2 months ago #

    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).

  8. dunar21
    Member
    Posted 2 months ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic

Tags