Support » How-To and Troubleshooting » [Resolved] Get Custom Post by its ID

[Resolved] Get Custom Post by its ID

  • Im really stuck on this .. \

    I have an ID of a custom post type, and on my page (of a different post type) I want to get the Featured image of my custom-post-type which I have the ID for ..

    I thought a simple WP_Query would do the job – but I cant seem to get access to the right info to pull out the_post_image() ..

    anyone help out with this – Ive seen to have gone round and round in circles.>!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Use get_post_thumbnail_id() to retrieve the id of the image, and then wp_get_attachment_image_src() to get an array of information about the image.

    thanks – but that will return me the current pages featured image ID .. and not the CPT post I am searching for via the ID I have for it …

    See what I mean – or do u mean – I do a wpquery for my the post, and then call the get_post_thumbnail_id in that query .. enabling me to pick up the thumbnail?

    Ill try indeed

    Just pass the id of the post whose featured image you want to retrieve to get_post_thumbnail_id() and then pass the return of that to wp_get_attachment_image_src().

    OK cracked it .. I did it this way round ..

    $terms=get_terms('post-type-name'); /* finds 'my' taxonomies associated to this post */
    $id = $terms[0]->term_id; /* grabs the 1st ID */
    $loop = new WP_Query( array('post_type' => 'post-type-name','p'=>$id)  );
    while ( $loop->have_posts() ) : $loop->the_post();

    Andrew – but yours is simpler – now that ive seen your entry .. will implement .. thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Get Custom Post by its ID’ is closed to new replies.