1

If I have two different post types - say, the standard WP post type and a custom post type, how do I go about pulling them both into one page, but into two different HTML containers?

For instance I'd like one post-type to show on my home page with the standard posts (I'd set this to show the top 5 posts)

while(have_posts()){
  the_post(); ?>

// Pulls in standard WP posts

<?php } ?>

And inside a different HTML container have a custom post type called 'features', that pulls in a 'features' custom post type (which would also show the most recent posts of this post-type).

while(have_posts()){
  the_post(); ?>

// Pulls in the 'features' custom post type

<?php } ?>

** NOTE - I know how to set up the CPT in my functions.php file, it's just how I'd pull them into the same page I'd like to know.

Thanks in advance for any suggestion.

Em

2

Updated 10 hours later: enriched with alternative solution and complemented with comments.

<?php
/* ******* Built-in post type "Post" ******* */
$arguments_for_posts = array(
    'numberposts' => 1, // number of posts to get
    'post_type'   => 'post' // post type
);

// get the array of post objects
$posts = get_posts( $arguments_for_posts );

// iterate through array to get individual posts
foreach( $posts as $post ) {
    // Enclosing block having unique ID
    echo '<article id="post-' . $post->ID . '">';
        // Post title (property of $post object)
        echo '<h1>' . $post->post_title . '</h1>';
        // Post content (property of $post object)
        echo $post->post_content;
    echo '</article>';
}

/* ******* Custom post type "Feature" ******* */
$arguments_for_features = array(
    'numberposts' => 1,
    'post_type'   => 'features'
);

$features = get_posts( $arguments_for_features );

foreach( $features as $feature ) {
    echo '<article id="feature-' . $feature->ID . '">';
        echo '<h1>' . $feature->post_title . '</h1>';
        echo $feature->post_content;
    echo '</article>';
}

Or you can use two WP_Queries when loops will look more familiar:

<?php
/* ******* Built-in post type "Post" ******* */
// Arguments
$args = array(
    'posts_per_page' => 1,
    'post_type'      => 'post',

);
// The query
$query_posts = new WP_Query( $args );

// The loop
while ( $query_posts->have_posts() ) {
    $query_posts->the_post();
?>
    <article id="post-<?php the_ID(); ?>">
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    </article>
<?php
} // END while()

// Essential!
// Set the post data back up
wp_reset_postdata();

/* ******* Custom post type "Feature" ******* */
$args = array(
    'posts_per_page' => 1,
    'post_type'      => 'feature',

);

$query_features = new WP_Query( $args );

while ( $query_features->have_posts() ) {
    $query_features->the_post();
?>
    <article id="feature-<?php the_ID(); ?>">
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    </article>
<?php
} // END while()
?>

Thanks @Qaisar Feroz for the useful reminder.

Useful links:

  • Could you give some explanation to that answer - I'm completely lost. – The Chewy Mar 16 '19 at 3:15
  • 1
    Great! short and simplest possible answer. However, it is suggested that, within every foreach statement id of div tag should change for each post/feature. I mean echo '<div id="post">' should be echo '<div id="post-' . $post->ID . '">' AND echo '<div id="feature">' should be echo '<div id="feature-' . $feature->ID . '">' – Qaisar Feroz Mar 16 '19 at 7:54
  • @TheChewy Done! – Max Yudin Mar 16 '19 at 9:03
  • @QaisarFeroz Already not short. And thanks for the suggestion! – Max Yudin Mar 16 '19 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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