0

I'm trying to do something very similar to this question: Have different number of posts on first page

The only difference is I need 10 posts on the first page of the blog and 9 posts on every other page.

I have the following setup:

  • Posts are displayed on a posts page (/news/)
  • home.php is being used for the blog page template and it uses the standard while loop for showing posts.
  • I have the posts per page setting in Settings > Reading set to 9.
  • I have used a portion of the code supplied by the answer in the link above (see snippet below). This is placed in my functions.php

Code added to functions.php:

function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $query->set( 'post_type', 'post' );
    $query->set( 'post_status', 'publish' );
    $query->set('ignore_sticky_posts', true);
    $ppp = get_option('posts_per_page'); // 9
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$offset);
    }
  }
}
add_action('pre_get_posts','tax_and_offset_homepage');

function homepage_offset_pagination( $found_posts, $query ) {
    $offset = 1;

    if( $query->is_home() && $query->is_main_query() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );

With all of this added, the first page shows 9 posts (as do all pages afterward). The tenth post does NOT show up on the first page, but it is also missing from the top of the second (the 10th most recent post never shows up), so I think I might be close.

Can anyone tell me what I am doing wrong?

EDIT: Looks like posts_per_page isn't being respected. I printed the $query inside the first if statement in the code above and this is what was returned:

WP_Query Object
(
    [query] => Array
        (
            [page] => 
            [pagename] => news
        )

    [query_vars] => Array
        (
            [page] => 
            [pagename] => news
            [error] => 
            [m] => 
            [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] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [title] => 
            [fields] => 
            [menu_order] => 
            [embed] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [post_name__in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

            [author__in] => Array
                (
                )

            [author__not_in] => Array
                (
                )

            [post_type] => post
            [post_status] => publish
            [ignore_sticky_posts] => 1
            [posts_per_page] => 10
        )

    [tax_query] => 
    [meta_query] => 
    [date_query] => 
    [queried_object] => WP_Post Object
        (
            [ID] => 1205
            [post_author] => 1
            [post_date] => 2017-10-13 14:51:41
            [post_date_gmt] => 2017-10-13 14:51:41
            [post_content] => 
            [post_title] => News
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => news
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2017-10-13 14:51:41
            [post_modified_gmt] => 2017-10-13 14:51:41
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://64.37.102.163/~mc5movie/?page_id=1205
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [queried_object_id] => 1205
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 1
    [is_404] => 
    [is_embed] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 1
    [is_post_type_archive] => 
    [query_vars_hash:WP_Query:private] => aea7eb467f9dbaa315c6d24870ff42f2
    [query_vars_changed:WP_Query:private] => 
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
    [compat_fields:WP_Query:private] => Array
        (
            [0] => query_vars_hash
            [1] => query_vars_changed
        )

    [compat_methods:WP_Query:private] => Array
        (
            [0] => init_query_flags
            [1] => parse_tax_query
        )

)

Any help is appreciated! I have been looking for a couple days and haven't found an answer to this.

0

There was a plugin interfering with the code (Jetpack infinite scroll). Upon disabling it, the first page of the blog correctly displayed 10 posts.

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.