user3181828
user3181828

Reputation: 381

Get custom post type by ID in Wordpress

I'm basically looking at somehow creating a loop so I can show the content of a single custom post type based on it's ID.

So, I want to get the content from the custom post type with an ID of 3788.

There is a function in there to get the featured image URL too.

For example, here's my code at the moment:

<?php $args = array( 'post_type' => 'about', 'posts_per_page' => 1 ); ?>

<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php global $post; ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>  
<div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">

<?php the_content () ?>

<?php endwhile; ?>

Upvotes: 2

Views: 45696

Answers (4)

Ayoub Bousetta
Ayoub Bousetta

Reputation: 316

$args = array(
'p' => $id_post, 
'post_type' => 'about', 
'post_status' => 'any, auto-draft','posts_per_page' => -1);
            $posts = get_posts($args);

            var_dump($posts);

You will not see the posts if they are in Draft or Trash so try to add post_status to your query args...took me while to notice that

Upvotes: 0

Brev Tiw
Brev Tiw

Reputation: 602

In case anyone else stumbles upon this question and wants an easy way to to do this, get_post_field is the function you are looking for. Say you have a post with an ID of 420. If you just want to get the post content for that post regardless of if you are in the loop or not, you could do:

<?php echo get_post_field( 'post_content', 420); ?>

I like doing it this way in some cases because it is just one line of code. You can do this for any of the fields that are included in a post object (post_title, post_excerpt, post_type, post_author, post_parent etc).

Now of course in your specific case you could have just done

<?php echo get_the_content( $post->ID ); ?>

But for those people with bad memory who can't remember all the functions like get_the_content(), get_the_title(), get_the_author(), get_the_modified_date() etc, you have a one liner that will do it all.

Upvotes: 0

Amit Kumar PRO
Amit Kumar PRO

Reputation: 1240

   <?php
   $ids= array(3788); // for example, You can also pass multiple IDs in array
   $args = array('post_type' => 'about','post__in' => $ids);
   $loop = get_posts($args);
   while ( $loop->have_posts() ) : $loop->the_post();
   global $post;

   $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 
   5600,1000 ), false, '' ); ?>  
   <div class="section" style="background: url(<?php echo $src[0]; ?>) no- 
   repeat center center fixed; -webkit-background-size: cover; -moz-background- 
   size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
   <?php the_content () ?>

   <?php endwhile; ?>

Upvotes: 2

<?php 
$ID = 3788;
$args = array('p' => $ID, 'post_type' => 'about');
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php global $post; ?>
    <?php
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ),  false, '' ); ?>  
    <div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center     center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
    <?php the_content () ?>
<?php endwhile; ?>

Upvotes: 6

Related Questions