I have several standard posts and posts from a custom posts type. Now I want to display all of them on my home page using a loop. My problem is that it displays the posts including the posts from the custom post type, but not all.

Types of posts I have right now: 7 standard posts, 15 Photos, 5 Videos, 2 Webs. Total of 29 posts.

In my Reading settings, I have set the blogs pages to show at most to 6 posts. Now when I go to the home page, I can see those 6 posts mixed with different post types but there are only 2 pages (the 2nd page also consist of 6 posts). Meaning, there are only less than 13 posts where it actuality, I have 29 total posts. When I go to website.com/page/3/ I get a 404 error.

I am using the latest FoundationPress framework for my WordPress theme and here's my code

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $loop = new WP_Query(array(
            'post_type' => array('post', 'photo', 'video', 'web'),
            'posts_per_page' => get_option('posts_per_page'),
            'paged' => $paged

        <?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>

Just for reference, here's the FoundationPress function to show the navigation

$paginate_links = paginate_links( array(
    'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
    'current' => max( 1, get_query_var( 'paged' ) ),
    'total' => $wp_query->max_num_pages,
    'mid_size' => 5,
    'prev_next' => true,
    'prev_text' => __( '&laquo;', 'foundationpress' ),
    'next_text' => __( '&raquo;', 'foundationpress' ),
    'type' => 'list',
) );
  • 1
    Use pre_get_posts to alter the main query. Drop the custom query – Pieter Goosen Jan 21 '16 at 9:52
  • Sadly, this is the only time I have read about pre_get_posts, but this seems to be the answer. – vanduzled Jan 21 '16 at 10:05
  • Then you would find this answer very useful. Enjoy ;-) – Pieter Goosen Jan 21 '16 at 10:19

You don't need a custom query to include CPTs in homepage posts in normal cases. Just use pre_get_posts.

function wpse215208_include_all_cpt( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set('post_type', array( 'post', 'photo', 'video', 'web' ) );
add_action('pre_get_posts', 'wpse215208_include_all_cpt');
  • I'm having an error on your code Fatal error: Call to a member function is_main_query() on a non-object on line 4 – vanduzled Jan 21 '16 at 10:04
  • @user1874974 My bad, missed the argument in callback. This is what happens when you type in hurry. I have fixed the code. Please try now. – Abhik Jan 21 '16 at 10:08
  • Sweet! Works like a charm. I have never heard about this pre_get_posts until now and it saved me tons. thanks! – vanduzled Jan 21 '16 at 10:10

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.