2

I'm trying to exclude the first 5 posts from the first page on the homepage, but I'm unable to get it right. This is the patern that I'd like to follow:

  • Page 1: Post 5, Post 6, Post 7, Post 8, Post 9
  • Page 2: Post 10, Post 11, Post 12, Post 13, Post 14
  • Page 3: Post 15, Post 16, ect.
  • Page n= Post (n-1), Post n, Post (n+1) (in math terms)

So, I tried to use pre_get_posts hook, and added the following to my functions.php file:

function my_function_for_excluding_posts( $query ) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set( 'offset', '5' );
    }
}
add_action( 'pre_get_posts', 'my_function_for_excluding_posts' );

This works in part, as it excludes the first five posts, but also repeats the same posts on every page, and thus not following the pattern I'm looking for.

This is my loop file, which basically does all the work, since my single.php just calls the loop file to handle everything, basically. This is the unaltered file, since I tried to add a new WP_Query, but didn't work well, so here it goes with no edits:

<?php 
    global $post, $query_string, $SMTheme;
    query_posts($query_string);
    $i=1;
    if (have_posts()) :  

    if (!isset($_GET['ajaxpage'])) {?>
 <div class='articles'>
    <?php }
    while (have_posts()) : the_post(); 
    ?>
        <div class='one-post'>
        <div id="post-<?php the_ID(); ?>" <?php post_class("post-caption"); ?>>     
            <?php if (!is_single()&&!is_page()) { ?>
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), the_title_attribute( 'echo=0' ) ); ?>" class='post_ttl'><?php the_title(); ?></a></h2>
            <?php } else { ?>
                <?php if (!is_single()) {?><h1 style="text-align:center;border-bottom:1px solid;margin-top:-10px;max-width: 100%;"><?php the_title(); ?></h1>
                <?php } else { ?><h1><?php the_title(); ?></h1>
            <?php } ?>
            <?php } ?>

            <?php if (!is_page()) {?><p class='post-meta'>
                <span class='post-date'><span class="day"><?php echo get_the_date('d'); ?></span><br /><span class="month"><?php echo get_the_date('M'); ?></span><br /><span class="year"><?php echo get_the_date('Y'); ?></span></span>           
                Publicado en &nbsp;<?php the_category(', '); ?>
                <?php if(comments_open( get_the_ID() ))  {
                    ?> &nbsp;| &nbsp;<?php comments_popup_link( 0, 1, '%' ); ?> &nbsp;Comentario(s)&nbsp;<?php
                } 
                edit_post_link( $SMTheme->_( 'edit' ), '     |     <span class="edit-link">&nbsp;', '</span>' );
                ?>
            </p><?php } ?>
                        <?php
                if(has_post_thumbnail())  {
                    ?><?php if (!is_single()) { ?><a href="<?php the_permalink(); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), the_title_attribute( 'echo=0' ) ); ?>"><?php the_post_thumbnail(
                        array($SMTheme->get( 'layout', 'imgwidth' ), $SMTheme->get( 'layout', 'imgheight' )),
                        array("class" => $SMTheme->get( 'layout','imgpos' ) . " featured_image")
                    ); ?></a><?php } else { ?>
                        <?php the_post_thumbnail(
                        array($SMTheme->get( 'layout', 'imgwidth' ), $SMTheme->get( 'layout', 'imgheight' )),
                        array("class" => $SMTheme->get( 'layout','imgpos' ) . " featured_image")
                    ); ?>
                    <?php }
                }
                ?>
        </div>
        <div class='post-body'>

                <?php
                if (!is_single()&&!is_page()) {
                    if ( ! post_password_required() ) { smtheme_excerpt('echo=1'); } else the_content('');
                    ?><a href='<?php the_permalink(); ?>' class='readmore'><?php echo $SMTheme->_( 'readmore' ); ?></a><?php
                } else {
                    the_content('');

                }
            ?>
        <?php if (is_single()) { ?>
        <div class="navigation">
                <div class="alignleft"> <?php previous_post_link('%link', '&larr; %title', true); ?></div>
                <div class="alignright"><?php next_post_link('%link', '%title &rarr;', true); ?></div>
        </div>
        <?php  } ?>         
            <?php wp_link_pages(); ?>           
        </div>      
        </div>

    <?php endwhile; ?>

    <?php if (!isset($_GET['ajaxpage'])) {?>
 </div>
    <?php } ?>


<?php endif; ?>

Any help or advice will be much appreciated.

4

offset overrides pagination, because when you get down to the query level, it's paginated via offset.

You can still use offset though, you just have to do some math to multiply your desired offset by the current page number (note that this calculation works because posts per page and offset are both 5, you may have to use the posts_per_page value in your calculation if the two are different):

function my_function_for_excluding_posts( $query ) {
    if ($query->is_home() && $query->is_main_query()) {
        $offset = 5;
        $paged = 0 == $query->get( 'paged' ) ? 1 : $query->get( 'paged' );
        $query->set( 'offset', $paged * $offset );
    }
}
add_action( 'pre_get_posts', 'my_function_for_excluding_posts' );

EDIT- filtering found_posts so number of pages is correct.

function myprefix_adjust_offset_pagination($found_posts, $query) {
    if ( $query->is_home() && $query->is_main_query() ) {
        return $found_posts - 5;
    }
    return $found_posts;
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
  • Thanks for your answer, Milo. The code works great, but it adds an extra last page on the paginations, and when I click on that page it gives me the 404 error. Is it posible to modify this a bit so this last page? Thanks. Regards – user1903782 Nov 2 '14 at 13:36
  • you'll either have to output the pagination links yourself, or filter found_posts to subtract your offset. Also see Making Custom Queries using Offset and Pagination – Milo Nov 2 '14 at 15:47
  • Thanks again. I'd prefer filtering found_posts, but I'm having a hard time figuring out how to do it. I added the found_posts filter, the same way the example on the codex does, below the pre_get_posts hook, but it doesn't change anything :/ – user1903782 Nov 2 '14 at 18:02
  • Any help? This is what I added to my functions.php file, but it didn't work. Here is the code: pastebin.com/5TR3jS9n. Thanks in advance! Regards. – user1903782 Nov 4 '14 at 10:16
  • See my edit above, working for me on my test site. – Milo Nov 4 '14 at 15:35

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.