Ready to get started?Download WordPress


[resolved] Get Custom Post by its ID (6 posts)

  1. vonKristoff
    Posted 1 year ago #

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

  2. Andrew Bartel
    Posted 1 year ago #

    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.

  3. vonKristoff
    Posted 1 year ago #

    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

  4. Andrew Bartel
    Posted 1 year ago #

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

  5. vonKristoff
    Posted 1 year ago #

    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();
  6. vonKristoff
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic