1

I'm trying to achieve something with the Loop and CPT. I've built a template page which will be the homepage of my website displaying articles and videos (the CPT). How can I add the CPT within a loop so it displays both in order as sort of a feed?

I've added the snippet below to functions.php :

// Custom loop feed
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'videos' ) );
    return $query;
}

And then put the code for the loop in my homepage.php.

Thing is, it displays it on my blog page, but on my homepage it made a post for the homepage.

enter image description here

Is there something I've done wrong?

0

There are a few ways to accomplish what you are trying to do. The first and easiest thing that I think I would try is to replace your call to is_home() with is_ftont_page(). Read here for the difference between the two.

If that doesn't fix your issue, you could try making a call to WP_query from within your template code. It might look something like this:

$query = new WP_query(
    array(
        'post_type' => array('post', 'videos'),
        'post_status' => 'publish',
        'order' => 'ASC'
    )
);
foreach ($query->posts as $post) {
    // Your display code here.
}

There are a lot of parameters that you can modify to get the query working just the way you want it. It's all there in the documentation. If you go this route, you would want to remove the 'pre_get_posts' hook.

Hope this helps!

  • Thanks for your quick reply Mike ! I've tried the first solution but it doesn't seem to work as i got the same result. it just display a post made as if the homepage was an article. As for the other solution i'm a bit confused on where i have to use the ?> to close the tags. Shall i still add the regular loop code ? – Laurent Feb 25 '16 at 21:10
  • I've tried this but code does not work : <?php $query = new WP_query( array( 'post_type' => array('post', 'videos'), 'post_status' => 'publish', 'order' => 'ASC' ) ); foreach ($query->posts as $post) { <?php if (have_posts()): while (have_posts()) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php endwhile; ?> <?php else: ?> <!-- article --> <article> <h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1> </article> <!-- /article --> <?php endif; ?> };?> – Laurent Feb 25 '16 at 21:18
  • It looks like you are trying to create a loop within a loop there. You do not need to do so. You can do something more like: foreach ($query->posts as $post) { ?> <article> <h1><?php the_title(); ?></h1> <?php the_content(); ?> </article> <?php } There is no need for the if (have_posts()): while (have_posts()) : the_post(); part. – Mike D Feb 25 '16 at 21:34
  • It got me an error 500. – Laurent Feb 25 '16 at 21:48
  • That could be a lot of things. Most likely a simple syntax error somewhere. It's hard to diagnose without seeing your full code. – Mike D Feb 25 '16 at 21:51

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.