functions - How to offset main query without affecting other queries and backend

admin2025-06-02  1

I'm trying to offset my main blog query by 3 posts whilst trying to get the pagination to function correctly. I've been following the codex reference here but I'm struggling to get it working as I want.

Basically I have 3 loops on my blog page, one to get the recent posts, the main loop that I want to offset by 3 and a popular posts loop. I currently have it working and everything is displaying correctly on the front end, however, in the backend it is offsetting my posts by 3 so 3 of them are missing (I assume because I'm targeting the main query). Here is my functions.php code:

// Offset Posts

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

add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {

    //Before anything else, make sure this is the right query...
    if ( ! $query->is_main_query()) {
        return;
    }

    //First, define your desired offset...
    $offset = 3;

    //Next, determine how many posts per page you want (we'll use WordPress's settings)
    $ppp = get_option('posts_per_page');

    //Next, detect and handle pagination...
    if ( $query->is_paged ) {

        //Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );

        //Apply adjust page offset
        $query->set('offset', $page_offset );

    }
    else {

        //This is the first page. Just use the offset...
        $query->set('offset',$offset);

    }
}

add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {

    //Define our offset again...
    $offset = 3;

    //Ensure we're modifying the right query object...
    if ( $query->is_home() && $query->is_main_query() ) {
        //Reduce WordPress's found_posts count by the offset... 
        return $found_posts - $offset;
    }
    return $found_posts;
}

Here is my code for the blog page:

<div class="container">
    <div class="slider-recent-posts" id="recent-posts">
        <?php $args= array (
            'posts_per_page' => 3
        ); ?>
        <?php $recent = new WP_QUERY($args); while($recent->have_posts()): $recent->the_post(); ?>
        <div class="slide">
            <div class="post">
                <div class="content">
                    <div class="blog-info">
                        <p><?php the_category(', '); ?></p>
                        <h3><?php the_title(); ?></h3>
                        <p><?php echo the_excerpt(); ?></p>
                        <a href="<?php the_permalink(); ?>" class="wo-btn full">Read more</a>
                    </div>
                </div>
                <div class="image">
                    <?php the_post_thumbnail('entry', array('class' => 'blog-img') ); ?>
                </div>
            </div>
        </div>
        <?php endwhile; wp_reset_postdata(); ?>
    </div>
</div>

<div class="container main-blog">
    <div class="row">
        <div class="col-md-8">
            <div class="row">

                    <?php while (have_posts()): the_post(); ?>
                    <div class="col-md-6">
                        <div class="blog-card">
                            <?php the_post_thumbnail('entry', array('class' => 'blog-img') ); ?>

                            <div class="entry-content">
                                <p class="category"><?php the_category(', '); ?></p>
                                <h3><?php the_title(); ?></h3>

                                <p><?php echo the_excerpt(); ?></p>

                                <a href="<?php the_permalink(); ?>" class="wo-btn full">Read more</a>
                            </div>
                        </div>
                    </div>

                    <?php endwhile; wp_reset_postdata();?>
            </div>



            <?php understrap_pagination(); ?>
        </div>
        <div class="col-md-4">
            <?php get_template_part( 'custom-side-bar' ); ?>
        </div>
    </div>
</div>

How can I get this working so it only targets the main loop on the blog page without affecting other queries or anything in the backend?

Any thoughts would be much appreciated!!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748832258a314093.html

最新回复(0)