Home page not obeying post per page setting on the first page only

I am working on a custom template for my office. I have a static front page and a static home page to display the blog posts. I am having a few minor issues and will have to create a few other questions as I fix each problem… Anyways, this problem is pretty interesting to me.

While in development, I have the post per page set to 3 (Settings > Reading). On the blog page (home.php) I do see my latest posts and that’s fine. The problem is the home page is showing about 8 blog posts instead of the 3 that it is set to display. I am not doing anything custom with the query and just using the loop to display the post content.

Read More

The pagination works fine and functions as expected All paginated pages display only 3 posts and they are all the correct posts for each page. The only issue with this is that the home page first shows 8 posts instead of the 3 that is set in the settings.

Any help on this is much appreciated. I tried searching and could not find anyone having this issue so sorry if this is a duplicate post and hopefully I can get a push in the right direction.

I also checked with the Twenty Eleven theme and the same issue occurs. This is the template I copied some logic from.

I disabled all plugins and the issue still exists. The only plugins I was using were the WordPress SEO and WP Lightbox 2. Even after disabling them the issue still occurs.

If I increase the posts per page to 15 it shows 17… If I increase it to 6 I show 10. If I increase the number to 7 or 8 I show 11 posts. If I set it to 9 I show 12. 10 shows 13…. 12 shows 14… As you can see very frustrating. The only place where I change the query is on the front page but I doubt that is affecting the home page. I basically do a custom query on the front page to show only the last 6 sticky posts. I make sure the reset the query afterwards to be safe. Still have no clue about why this is happening. This has me going nuts!

I have provided my code below:

home.php

<?php 
/*
Template Name: Home Page (Blog Roll)
*/
?>

<?php get_header(); ?>

<div id="main">
    <div id="content" class="list-box">
        <?php
        /* Run the loop to output the page.
        * If you want to overload this in a child theme then include a file
        * called loop-page.php and that will be used instead.
        */
        get_template_part( 'loop', 'single' );
        ?>
        <div class="page-nav">
            <?php if (get_next_posts_link()): ?>
            <span class="nav-button older-posts"><?php next_posts_link( '&laquo; Older posts'); ?></span>
            <?php 
            endif;
            if (get_previous_posts_link()): ?>
            <span class="nav-button newer-posts"><?php previous_posts_link( 'Newer posts &raquo;' ); ?></span>
            <?php endif; ?>
        </div>        
    </div>
    <?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>

loop-single.php

<?php
/**
* The loop that displays a single post.
*
* The loop displays the posts and the post content. See
* http://codex.wordpress.org/The_Loop to understand it and
* http://codex.wordpress.org/Template_Tags to understand
* the tags used in it.
*
* This can be overridden in child themes with loop-single.php.
*
* @package WordPress
* @subpackage EHRScopev2
*/
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="box">
            <div class="box-holder">
                <div class="box-frame">
                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                        <em class="meta"><?php ehrscope_posted_on(); ?>&nbsp;|&nbsp;<span class="total-comments"><?php comments_popup_link(); ?></span></em>
                        <?php
                        if (!is_single() && has_excerpt()):
                            the_excerpt();
                        else:
                            the_content(__('Continue reading', 'ehrscope') . ': <em>' . the_title('', '', false) . '</em>');
                        endif;
                        ?>
                        <div class="clear"></div>
                        <?php
                        if (is_single()):
                            ehrscope_wp_link_pages();
                            $tags = wp_get_post_tags($post->ID);
                            ?>
                            <p class="entry-meta"><?php ehrscope_show_tags(); ?></p>
                            <?php if ($tags): ?>
                                <a href="javascript:void(0);" id="toggle-related-posts" class="button"><?php _e('Show Related Posts', 'ehrscope'); ?></a>
                                <?php
                            endif;
                        endif;
                        ?>
                    </div>
                </div>
            </div>
        </div>

        <?php if (is_single()): ?>
            <div class="box" id="related-posts-cage">
                <div class="box-holder">
                    <div class="box-frame">
                        <div>
                            <?php if (is_single()): ?>
                                <?php
                                if ($tags):
                                    $tag_ids = array();
                                    foreach ($tags as $individual_tag) {
                                        $tag_ids[] = $individual_tag->term_id;
                                    }
                                    $args = array(
                                        'tag__in' => $tag_ids,
                                        'post__not_in' => array($post->ID),
                                        'posts_per_page' => 10, // Number of related posts to display.
                                        'caller_get_posts' => 1
                                    );
                                    $my_query = new WP_Query($args);
                                    if ($my_query->have_posts()):
                                        ?>
                                        <h3><?php _e('Related Posts', 'ehrscope'); ?></h3>
                                        <ul>
                                            <?php
                                            while ($my_query->have_posts()) : $my_query->the_post();
                                                ?>
                                                <li>
                                                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                                                    &nbsp;
                            <!--                                                <span class="related-posts-date"><?php the_time('F jS, Y'); ?></span>-->
                                                </li>
                                                    <?php
                                            endwhile;
                                            ?>
                                        </ul>
                                        <?php
                                    endif;
                                endif;
                            endif;
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        endif;
        comments_template('', true);
    endwhile;
else:
    ?>
    <div class="box">
        <div class="box-holder">
            <div class="box-frame">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <h2><?php _e('No Results', 'ehrscope'); ?></h2>
                    <p><?php _e('Sorry, no posts matched your criteria.', 'ehrscope'); ?></p>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>

Contents of $wp_query:

Array
(
    [page] => 0
    [pagename] => blog
    [error] => 
    [m] => 0
    [p] => 0
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0
    [name] => 
    [static] => 
    [page_id] => 0
    [second] => 
    [minute] => 
    [hour] => 
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] => 
    [tag] => 
    [cat] => 
    [tag_id] => 
    [author_name] => 
    [feed] => 
    [tb] => 
    [paged] => 0
    [comments_popup] => 
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [fields] => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [update_post_term_cache] => 1
    [update_post_meta_cache] => 1
    [post_type] => 
    [posts_per_page] => 3
    [nopaging] => 
    [comments_per_page] => 5
    [no_found_rows] => 
    [order] => DESC
)

Related posts

Leave a Reply

2 comments

  1. Do a search through your theme’s entire directory for the functions “is_home()” and “is_front_page()”. The only thing I can think of is that there’s a function written somewhere that makes the front page have 8 posts no matter how many posts are set per page on the rest of the blog.

  2. It seems this was a noobie mistake.

    The sticky posts are added to the top of the page thus increasing the post count on the initial blogroll. I just had to remove the sticky posts from being sticky and that solved the issue.

    I only use the sticky posts for the front page and I do not want them to be at the top of the home.php page.

    Thanks for your help!

    Updated home.php

    <?php
    /*
    Template Name: Home Page (Blog Roll)
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="main">
        <div id="content" class="list-box">
            <?php
            // Prevents sticky posts form always being at the top of the page. (still shows them in normal sort order)
            $queryObject = new WP_Query(array(
                'caller_get_posts' => '1',
                'paged' => get_query_var('paged')
            ));        
            /* Run the loop to output the page.
            * If you want to overload this in a child theme then include a file
            * called loop-page.php and that will be used instead.
            */
            get_template_part('loop', 'custom-query');
            ehrscope_posts_page_navigation();
            ?>
        </div>
        <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>
    

    loop-custom-query.php

    <?php
    /**
    * The loop that displays a single post.
    *
    * The loop displays the posts and the post content. See
    * http://codex.wordpress.org/The_Loop to understand it and
    * http://codex.wordpress.org/Template_Tags to understand
    * the tags used in it.
    *
    * This can be overridden in child themes with loop-single.php.
    *
    * @package WordPress
    * @subpackage EHRScopev2
    */
    global $queryObject;
    ?>
    <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
            <div class="box">
                <div class="box-holder">
                    <div class="box-frame">
                        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                            <em class="meta"><?php ehrscope_posted_on(); ?>&nbsp;|&nbsp;<span class="total-comments"><?php comments_popup_link(); ?></span></em>
                            <?php
                            if (!is_single() && has_excerpt()):
                                the_excerpt();
                            else:
                                the_content(__('Continue reading', 'ehrscope') . ': <em>' . the_title('', '', false) . '</em>');
                            endif;
                            ?>
                            <?php
                            if (is_single()):
                                ehrscope_wp_link_pages();
                                $tags = wp_get_post_tags($post->ID);
                                ?>
                                <p class="entry-meta"><?php ehrscope_show_tags(); ?></p>
                                <?php if ($tags): ?>
                                    <a href="javascript:void(0);" id="toggle-related-posts" class="button"><?php _e('Show Related Posts', 'ehrscope'); ?></a>
                                    <?php
                                endif;
                            endif;
                            ?>
                        </div>
                    </div>
                </div>
            </div>
    
            <?php if (is_single()): ?>
                <div class="box" id="related-posts-cage">
                    <div class="box-holder">
                        <div class="box-frame">
                            <div>
                                <?php if (is_single()): ?>
                                    <?php
                                    if ($tags):
                                        $tag_ids = array();
                                        foreach ($tags as $individual_tag) {
                                            $tag_ids[] = $individual_tag->term_id;
                                        }
                                        $args = array(
                                            'tag__in' => $tag_ids,
                                            'post__not_in' => array($post->ID),
                                            'posts_per_page' => 10, // Number of related posts to display.
                                            'caller_get_posts' => 1
                                        );
                                        $my_query = new WP_Query($args);
                                        if ($my_query->have_posts()):
                                            ?>
                                            <h3><?php _e('Related Posts', 'ehrscope'); ?></h3>
                                            <ul>
                                                <?php
                                                while ($my_query->have_posts()) : $my_query->the_post();
                                                    ?>
                                                    <li>
                                                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                                                        &nbsp;
                                <!--                                                <span class="related-posts-date"><?php the_time('F jS, Y'); ?></span>-->
                                                    </li>
                                                        <?php
                                                endwhile;
                                                ?>
                                            </ul>
                                            <?php
                                        endif;
                                    endif;
                                endif;
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php
            endif;
            comments_template('', true);
        endwhile;
    else:
        ?>
        <div class="box">
            <div class="box-holder">
                <div class="box-frame">
                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <h2><?php _e('No Results', 'ehrscope'); ?></h2>
                        <p><?php _e('Sorry, no posts matched your criteria.', 'ehrscope'); ?></p>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>