Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I get a custom post by its ID? I want to display a single post from custom post using its ID. I tried 'post_type' => 'homepage', 'post_id' => '717' but it didnt work.

Thanks

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You simply don't need to specify a post_type when calling get_post(). The ID for any sort of post is unique among the whole DB-posts table. So if you're calling a post with ID = 17

$id = 17;
$post = get_post( $id );

then you'll simply get this single post.

Note, according the Codex, when using get_post

You must pass a variable containing an integer (e.g. $id). A literal integer (e.g. get_post(7)) will cause a fatal error (Only variables can be passed for reference or Cannot pass parameter 1 by reference).

share|improve this answer
    
Hi, I tried using below code but it didnt help. am I missing something here? <?php query_posts('p=794'); if(have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <div class="advanced-image"><?php echo get_the_post_thumbnail($page->ID, 'home-vertical'); ?></div> <p><?php the_content(); ?></p> <h3><a href="/about/">Learn More</a></h3> <?php endif;?> </div> I did same with other posts as well. Thanks –  FlourishDNA Jul 2 '12 at 21:07
    
What's that? Why making a loop (for a SINGLE post!?!) instead using a simple output of the data we showed you above? –  kaiser Jul 2 '12 at 22:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.