• Hello,

    I am using your plugin and overall it works well, but I am facing an issue with the Posts Widget pagination when using the Offset option.

    On my category/tag archive page, I have created two post lists:

    1. Top section – displays the latest single post.
    2. Bottom section – displays the remaining posts with pagination.

    To exclude the first post from the second list, I used the Offset = 1 option in the query settings. However, when Offset is set to 1, the pagination does not work correctly. When I navigate to page 2, 3, etc., it keeps loading the same posts instead of the correct paginated results.

    If I remove the Offset or set it to 0, the pagination works normally.

    I am also using the Custom Query ID option with the following code to filter posts based on the current taxonomy term:

    function bpfwe_by_post_status( $query ) {
        $term = get_queried_object();
        if ( $term && isset( $term->taxonomy ) ) {
            $tax_query[] = $query->get('tax_query');
            $tax_query[] = [
                'taxonomy' => $term->taxonomy,
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            ];
            $query->set( 'tax_query', $tax_query );
        }
    }
    add_action( 'bpfwe/query/my_custom_filter', 'bpfwe_by_post_status' );
    
    function bpfwe_by_post_status_new( $query ) {
        $term = get_queried_object();
        if ( $term && isset( $term->taxonomy ) ) {
            $tax_query[] = $query->get('tax_query');
            $tax_query[] = [
                'taxonomy' => $term->taxonomy,
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            ];
            $query->set( 'tax_query', $tax_query );
        }
    }
    add_action( 'bpfwe/query/my_custom_filter_new', 'bpfwe_by_post_status_new', 10, 1 );
    

    So my question is:

    • Is there a recommended way to exclude the first post while keeping pagination working correctly in the Posts Widget?
    • Or is there a known issue with the Offset option affecting pagination?

    Looking forward to your guidance.

    Thank you.

Viewing 1 replies (of 1 total)
  • Plugin Support Dara

    (@dara4)

    Hi Jayvirsinh Rajput (@jvrajput420),

    Thank you for the detailed explanations. This is actually a known limitation rather than a bug. When you use the offset parameter, WordPress does not automatically account for it during pagination calculations.

    Using the same query filter, you can achieve this by excluding the first posts from the second widget while keeping pagination intact. Instead of relying on Offset, you can pass the post ID of the first few posts as a post__not_in argument.

    Here is an example (in this case, the query would skip the first 3 posts):

    function bpfwe_by_post_status_new( $query ) {
        static $is_running = false;
    
        // Bail early to prevent recursion.
        if ( $is_running ) {
            return;
        }
    
        $term = get_queried_object();
    
        if ( $term && isset( $term->taxonomy ) ) {
            $tax_query   = (array) $query->get( 'tax_query' );
            $tax_query[] = array(
                'taxonomy' => $term->taxonomy,
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            );
            $query->set( 'tax_query', $tax_query );
        }
    
        // Set the flag before running the inner query.
        $is_running = true;
    
        $latest = get_posts(
            array(
                'posts_per_page'         => 1,
                'tax_query'              => array(
                    array(
                        'taxonomy' => $term->taxonomy,
                        'field'    => 'term_id',
                        'terms'    => $term->term_id,
                    ),
                ),
                'fields'                 => 'ids',
                'no_found_rows'          => true,
            )
        );
    
        // Reset the flag after the inner query completes.
        $is_running = false;
    
        if ( ! empty( $latest ) ) {
            $query->set( 'post__not_in', $latest );
        }
    }
    add_action( 'bpfwe/query/my_custom_filter_new', 'bpfwe_by_post_status_new', 10, 1 );

    With this filter in place, you could remove the offset and the pagination math would stay correct.

    Hope that helps. Let me know if you have any questions.

    Dara

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.