WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Review of Multiple Loop Pagination for Correction (4 posts)

  1. AMEEKER
    Member
    Posted 6 months ago #

    http://pastebin.com/HEGqDbcT

    In the paste above is the code for the template running the CPT archive page at http://www.m.angiemeekerdesigns.com/totd

    Everything about the page is working as expected except the pagination. What would like to happen is that when "Next Page" is clicked, Page 2 and forwards show the next posts from the CPT archive that were not also shown in one of the two loops on the first page.

    SO, I would not like for the first loop to repeat on subsequent pages, and I do not want subsequent pages to have the first post offset, and since there are 11 total posts on page 1 of this archive, I'd like the next page to not show those first 11 posts.

    Right now what is happening is that it is not showing the first loop (good), it is not offsetting the first post (good), but it is showing all 11 posts from the first page - for page 2 and every page after.

    Whew!

    Can this archive be corrected to paginate as I'd like? I have read MANY other posts and the codex on pagination for loops, and multiple loops (that's how I got to what I have), but alas, what I have is not working.

  2. keesiemeijer
    moderator
    Posted 6 months ago #

    WordPress will ignore pagination for a query if an offset is used.
    http://codex.wordpress.org/Function_Reference/WP_Query#Pagination_Parameters

    Here is a workaround.
    http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

    Another way is by storing the post id of the first loop and exclude if from the second.
    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    Or by combining the two loops. Example:

    global $wp_query;
    
    // Set up your query here
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = array(
    	'post_type'      => 'tipoftheday',
    	'orderby'        => 'date',
    	'posts_per_page' => '10',
    	'paged'  => $paged,
    );
    
    $wp_query = new WP_Query( $query );
    // the loop
    if ( $wp_query->have_posts() ) :
    
    	while ( $wp_query->have_posts() ): $wp_query->the_post();
    
    	// counter
    	if ( !is_paged() && $wp_query->current_post == 0 ) :
    
    			// do stuff for first post on the first page
    
    		else :
    
    			// do stuff for all other posts
    
    		endif;
    
    	endwhile;
                    // Pagination! Woo!
                    genesis_posts_nav();
    endif;
    // Reset the query
    wp_reset_postdata();
  3. AMEEKER
    Member
    Posted 6 months ago #

    Thank you for the help. The code below (which I've used from the first workaround), has corrected the pagination (fantastic!) but is not offsetting the first post in the second loop.

    /** Replace the standard loop with our custom loop */
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'totd_do_loop');
    add_action( 'genesis_loop', 'totd_teasers_do_loop');
    
    add_action('pre_get_posts', 'myprefix_query_offset', 1 );
    function myprefix_query_offset(&$query) {
    
        //First, define your desired offset...
        $offset = 0;
    
        //Next, determine how many posts per page you want (we'll use WordPress's settings)
        $ppp = 11;
    
        //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 = 0;
    
        //Ensure we're modifying the right query object...
            //Reduce WordPress's found_posts count by the offset...
            return $found_posts - $offset;
        }
    
    add_action('genesis_loop', 'totd_do_loop');
    function totd_do_loop() {
    if ( !is_paged() ) {
    	global $wp_query;
    	// Set up your query here
    	$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    	$query = array(
    		'post_type'      => 'tipoftheday',
    		'orderby'        => 'date',
    		'posts_per_page' => '1',
    
    	);
    
    	$wp_query = new WP_Query( $query );
    
    	if( $wp_query->have_posts() ):
    		while( $wp_query->have_posts() ): $wp_query->the_post();
    		$do_not_duplicate = $post->ID;
    			// Get the custom fields
    			        // Store the pre tips data
            $tips_data_pre = array(
                    'totd_tags' => get_field( 'totd_tags' ),
                    'tip_article_headline' => get_field( 'tip_article_headline' ),
                    'article_author' => get_field( 'article_author' ),
                    'article_author_link' => get_field( 'article_author_link' ),
            );
    
    // Only output if we have tips data
            if ($tips_data_pre['totd_tags'] != '' ||
                    $tips_data_pre['tip_article_headline'] != '' ||
                    $tips_data_pre['article_author'] != '' ||
                    $tips_data_pre['article_author_link'] != '') {
                   echo '<span class="date time published" title="%1$s">' , do_shortcode('[post_date]'),'</span>' ;
    
                    echo '<div class="tip-excerpt"><p><div class="entry-content">';
                   echo '<div class=h1 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>';
    						   echo the_excerpt();
                                    echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ;
                                    echo '<div class="entry-terms">
                                                                                    <div class="share">Share This Tip:</div>
                                                                                            <div class="addthis_toolbox addthis_default_style">
                                                                                                    <a class="addthis_button_preferred_1"></a>
                                                                                                    <a class="addthis_button_preferred_2"></a>
                                                                                                    <a class="addthis_button_preferred_3"></a>
                                                                                                    <a class="addthis_button_preferred_4"></a>
                                                                                                    <a class="addthis_button_compact"></a>
                                                                                                    <a class="addthis_counter addthis_bubble_style"></a>
                                                                                            </div>
                                                                                            <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script>
                                                                                    </div>
                                                                    </div></div>';
    
                    echo '</p>';
            }
    
    		endwhile;
    
    	endif;
    	// Reset the query
    	wp_reset_query();
    	}
    }
    
    add_action('genesis_loop', 'totd_teasers_do_loop');
    function totd_teasers_do_loop() {
    	global $wp_query;
    	// Set up your query here
    	$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 0;
    	$query = array(
    		'post_type'      => 'tipoftheday',
    		'orderby'        => 'date',
    		'posts_per_page' => '10',
    		'offset'		=> '1',
    		'paged'  => $paged,
    	);
    
    	$wp_query = new WP_Query( $query );
    
    	if( $wp_query->have_posts() ):
    		while( $wp_query->have_posts() ): $wp_query->the_post();
    
    			// Get the custom fields
    			        // Store the pre tips data
            $tips_data_pre = array(
                    'totd_tags' => get_field( 'totd_tags' ),
                    'tip_article_headline' => get_field( 'tip_article_headline' ),
                    'article_author' => get_field( 'article_author' ),
                    'article_author_link' => get_field( 'article_author_link' ),
            );
    
    // Only output if we have tips data
            if ($tips_data_pre['totd_tags'] != '' ||
                    $tips_data_pre['tip_article_headline'] != '' ||
                    $tips_data_pre['article_author'] != '' ||
                    $tips_data_pre['article_author_link'] != '') {
    				echo '<div class="time-teaser">
    				<span class="day">',get_the_time( 'l' ),'</span>
    				<span class="month">',get_the_time( 'm/d/Y' ),'</span> </div>';
    
                    echo '<div class="tip-excerpt"><p><div class="entry-content">';
    echo '<div class=h2 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>';
                    $excerpt = get_the_excerpt();
      echo string_limit_words($excerpt,25); echo '<a class="moretag" href="'. get_permalink($post->ID) . '"> Continue Reading&rarr;</a>';
                                    echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ;
                                    echo '<div class="entry-terms"></div>
                                                                    </div></div>';
    
                    echo '</p>';
            }
    
    		endwhile;
    		// Pagination! Woo!
    		genesis_posts_nav();
    	endif;
    	// Reset the query
    	wp_reset_query();
    }
  4. AMEEKER
    Member
    Posted 6 months ago #

    Here is the final code that worked:

    /** Replace the standard loop with our custom loop */
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'totd_do_loop');
    add_action( 'genesis_loop', 'totd_teasers_do_loop');
    
    add_action('pre_get_posts', 'myprefix_query_offset', 1 );
    function myprefix_query_offset(&$query) {
    
        //First, define your desired offset...
        $offset = $query->get('offset');
    
        //Next, determine how many posts per page you want (we'll use WordPress's settings)
        $ppp = 10;
    
        //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 = $query->get('offset');
    
            //Reduce WordPress's found_posts count by the offset...
            return $found_posts - $offset;
        }
    
    add_action('genesis_loop', 'totd_do_loop');
    function totd_do_loop() {
    if ( !is_paged() ) {
    	global $wp_query;
    	// Set up your query here
    	$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    	$query = array(
    		'post_type'      => 'tipoftheday',
    		'orderby'        => 'date',
    		'posts_per_page' => '1',
    
    	);
    
    	$wp_query = new WP_Query( $query );
    
    	if( $wp_query->have_posts() ):
    		while( $wp_query->have_posts() ): $wp_query->the_post();
    			// Get the custom fields
    			        // Store the pre tips data
            $tips_data_pre = array(
                    'totd_tags' => get_field( 'totd_tags' ),
                    'tip_article_headline' => get_field( 'tip_article_headline' ),
                    'article_author' => get_field( 'article_author' ),
                    'article_author_link' => get_field( 'article_author_link' ),
            );
    
    // Only output if we have tips data
            if ($tips_data_pre['totd_tags'] != '' ||
                    $tips_data_pre['tip_article_headline'] != '' ||
                    $tips_data_pre['article_author'] != '' ||
                    $tips_data_pre['article_author_link'] != '') {
                   echo '<span class="date time published" title="%1$s">' , do_shortcode('[post_date]'),'</span>' ;
    
                    echo '<div class="tip-excerpt"><p><div class="entry-content">';
                   echo '<div class=h1 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>';
    						   echo the_excerpt();
                                    echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ;
                                    echo '<div class="entry-terms">
                                                                                    <div class="share">Share This Tip:</div>
                                                                                            <div class="addthis_toolbox addthis_default_style">
                                                                                                    <a class="addthis_button_preferred_1"></a>
                                                                                                    <a class="addthis_button_preferred_2"></a>
                                                                                                    <a class="addthis_button_preferred_3"></a>
                                                                                                    <a class="addthis_button_preferred_4"></a>
                                                                                                    <a class="addthis_button_compact"></a>
                                                                                                    <a class="addthis_counter addthis_bubble_style"></a>
                                                                                            </div>
                                                                                            <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script>
                                                                                    </div>
                                                                    </div></div>';
    
                    echo '</p>';
            }
    
    		endwhile;
    
    	endif;
    	// Reset the query
    	wp_reset_query();
    	}
    }
    
    add_action('genesis_loop', 'totd_teasers_do_loop');
    function totd_teasers_do_loop() {
    	global $wp_query;
    	// Set up your query here
    	$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 0;
    	$query = array(
    		'post_type'      => 'tipoftheday',
    		'orderby'        => 'date',
    		'posts_per_page' => '10',
    		'offset'		=> '1',
    		'paged'  => $paged,
    	);
    
    	$wp_query = new WP_Query( $query );
    
    	if( $wp_query->have_posts() ):
    		while( $wp_query->have_posts() ): $wp_query->the_post();
    
    			// Get the custom fields
    			        // Store the pre tips data
            $tips_data_pre = array(
                    'totd_tags' => get_field( 'totd_tags' ),
                    'tip_article_headline' => get_field( 'tip_article_headline' ),
                    'article_author' => get_field( 'article_author' ),
                    'article_author_link' => get_field( 'article_author_link' ),
            );
    
    // Only output if we have tips data
            if ($tips_data_pre['totd_tags'] != '' ||
                    $tips_data_pre['tip_article_headline'] != '' ||
                    $tips_data_pre['article_author'] != '' ||
                    $tips_data_pre['article_author_link'] != '') {
    				echo '<div class="time-teaser">
    				<span class="day">',get_the_time( 'l' ),'</span>
    				<span class="month">',get_the_time( 'm/d/Y' ),'</span> </div>';
    
                    echo '<div class="tip-excerpt"><p><div class="entry-content">';
    echo '<div class=h2 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>';
                    $excerpt = get_the_excerpt();
      echo string_limit_words($excerpt,25); echo '<a class="moretag" href="'. get_permalink($post->ID) . '"> Continue Reading&rarr;</a>';
                                    echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ;
                                    echo '<div class="entry-terms"></div>
                                                                    </div></div>';
    
                    echo '</p>';
            }
    
    		endwhile;
    		// Pagination! Woo!
    		genesis_posts_nav();
    	endif;
    	// Reset the query
    	wp_reset_query();
    }

Reply

You must log in to post.

About this Topic