Forums

[resolved] Question about galleries (2 posts)

  1. kadorrna
    Member
    Posted 5 months ago #

    I am trying to get an specific image of a post (they are added as gallery).
    The thing is that I tried this 2 things:

    $image = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    This works fine but it is bringing me all the images. So I added the arg "post_title" with my image title, but it still brings me all the images.

    Later on I tried with wp_query:

    $image = new WP_Query( array(	'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image/png', 'post_title' => 'title' ) );

    But this is not working neither.
    It can't be so difficult, I want an specific image of a post by title...

    Can somebody give me a hint?

    Thanks in advance.

  2. kadorrna
    Member
    Posted 5 months ago #

    I have found this way:

    function getPostImageByTitle($postid, $imageName,$orderBy="DESC"){
    	global $wpdb;
    
    	$querystr = "SELECT ".$wpdb->posts.".* FROM ".$wpdb->posts." WHERE ".$wpdb->posts.".post_parent = ".$postid.
    	    " AND ".$wpdb->posts.".post_type = 'attachment'
    	    AND (".$wpdb->posts.".post_mime_type = 'image/png'
    	    		OR ".$wpdb->posts.".post_mime_type = 'image/gif' OR ".$wpdb->posts.".post_mime_type = 'image/jpeg')
    	    AND ".$wpdb->posts.".post_status = 'inherit'
    	    AND ".$wpdb->posts.".post_title = '".$imageName."'
    	    ORDER BY $wpdb->posts.post_date ".$orderBy;
    
    	return $wpdb->get_results($querystr, OBJECT);
    }

    I will leave this as not resolved for a couple of days looking forward for other ideas. If nobody gives any other solution I will close it.

    Cheers!

Reply

You must log in to post.

About this Topic