0

In the paste below 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.

<?php

/**
 *
 * Display the Tip of the Day Custom Post Type archive custom fields using
 * ACF.
 *
 * @author Angie Meeker
 * @uses   Advanced Custom Fields
 */

add_action('genesis_entry_content','genesis_do_post_title', 2);





 //* Add Tip of the Day body class to the head
add_filter( 'body_class', 'add_tiparchives_body_class' );
function add_tiparchives_body_class( $classes ) {
   $classes[] = 'tiparchives-post-type-archive-{tipoftheday}';
   return $classes;
}

 // Return Category and Tip of the Day on Single Posts
add_action ( 'genesis_before_content', 'show_totd', 9 );
function show_totd() {
        echo '<div class="totd-cats-title">Tip of the Day</div>';
}
  // Remove Post Author
add_filter( 'genesis_post_info', 'remove_post_author_totd_posts' );
function remove_post_author_totd_posts($post_info) {
    $post_info = '[post_date]';
    return $post_info;
}


/** 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('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();
}


genesis();

EDITED

I've come back to show my progress which used most of that same code offered by @mneil below (someone also mentioned in it the WP.org forums). The pagination does work correctly, and the first post is showing correctly in the first loop, but now the second loop is not offsetting properly (not skipping that latest post - the same shown in the first loop). Besides that though, this does work. Any thoughts on why the second loop would now skip the offset post?

   /** 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();
}
  • 1
    Just wanted to check this out but found a PHP fatal error.. Can you please fix thsi and get back? :) – fischi Feb 4 '14 at 20:47
  • @fischi I repasted the code from the template that is working on the live site. It a Genesis child theme, fyi. – angiemeeker Feb 4 '14 at 22:04
  • @fischi I see now what you meant now. The error on the page is fixed - the code above was correct, but I'd forgot to include a function in functions.php that's relevant (actually, I had it but then took it out real quick to test and forgot to put it back in before I went to get my daughter from school). SO, it's all back in and working. Well, except for the original issue of pagination. – angiemeeker Feb 4 '14 at 22:12
1

First off, to me it looks like the first loop just grabs the first posts of post type tip of the day, and then the second loop grabs the rest minus the first, so unless I am misunderstanding the function of your loops, I would just do 1 loop, check if it is the first iteration, if so, grab the separate data for the first post, then on all other iterations grab the data you need for the rest. Also as a double security measure, when checking if its the first iteration of the loop, also check to make sure it is not paged, i.e. it is on the first page of results, not page 2, 3 etc. That should just be a simple check of $wp_query->is_paged

DISCLAIMER: Upon reaching basically the end of writing this long answer, I realized that it pretty much will not work because filtering into pre_get_posts affects all queries including the get_posts() function. I decided to leave the full answer as some of its information may help you and possibly editing it around a little bit can solver your issue. However I believe what I wrote above is the simplest solution unless I misunderstand your intentions completely.

However, I will assume you need two seperate loops for other reasons, so on to my answer. I didnt test any of this so you'll have to test it out and see if it works, but the issue is that you are using offest, which will break pagination. Wordpress suggests that you have to handle pagination manually. http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

Also im going to assume that the second loop that only has posts per page = 1 is that you just want the first post on the archive to be that type etc.

First lets solve the pagination issue with offset following wordpress advice (modified for you).

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

    // Before anything else, make sure this is the right page...
    // Assuming this is a page. if its an archive, etc. choose the correct conditional
    // tag
    if ( ! is_page('totd') ) {
        return;
    }

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

    //Next, determine how many posts per page you want 
    $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);

    }
}

Next wordpress tells us to modify the found posts variable since it doesnt take into account your offset

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

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

    // make sure this is the right page...
    // Assuming this is a page. if its an archive, etc. choose the correct conditional
    // tag
    if ( is_page('totd') ) {
        //Reduce WordPress's found_posts count by the offset... 
        return $found_posts - $offset;
    }
}

Now im pretty sure that since pre_get_posts fires for every posts query, and we are checking if its the totd page, it would fire for both you're loops, so what I would do is now filter into the_posts and just insert that post into front of the $posts array.

function myprefix_add_first_post($posts){
    global $wp_query;

    // make sure this is the right page...
    // Assuming this is a page. if its an archive, etc. choose the correct conditional
    // tag, as well as make sure we are not on page 2, 3 etc of pagination
    if(!is_admin() && is_page('totd') && !($wp_query->is_paged)){
        $query_args = array(
                'post_type'      => 'tipoftheday',
                'orderby'        => 'date',
                'posts_per_page' => '1',

            );
        $first_post = get_posts($query_args);
        $all_posts = array_unshift($posts, $first_post[0])
        return $all_posts;
    }
    else{
        return $posts;
    }
}
add_filter('the_posts', 'myprefix_add_first_post', 1);

This should grab the first post and throw it in. You will still have to check for the first iteration of your loop and style it accordingly. (WARNING: as disclaimer claimed at top, pre_get_posts will still filter the get_posts(), so the above will probably not work without further editing.)

  • I have used the same code you presented to make progress - only the second loop is not offsetting the 1 post from the first loop. Besides that, it's working perfectly. Any thoughts? – angiemeeker Feb 5 '14 at 20:18
  • I'm not sure what you mean. Can you explain? – mneil Feb 5 '14 at 20:24
  • Ah. Sorry, I had to repaste my edit. Ok so the edit above produces this: m.angiemeekerdesigns.com/totd See how the first post (which is what's in the first loop) is being replicated in the second loop? – angiemeeker Feb 5 '14 at 20:27
  • I believe its because in the code you added that i supplied, you set the $offset variable to 0, so it runs the filter, and sets the $offset to 0 for the second loop even though you defined it as 1 in your $args. What I would do is try changing it to $offset = $query->get('offset'); (you must do this in both the pre_get_posts filter as well as the found_posts filter) and then in your first loop where you set posts per page = 1, set 'offset'=>'0' (still keeping posts per page = 1 of course, just using that to differentiate your loops) – mneil Feb 6 '14 at 1:05
0

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();
}

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.