Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Troy A.
Troy A.
13,069 Points

My custom query pagination needs help...

Hello, I need some help with this issue...normal pagination isn't working on my custom page templates when using the "Advanced Custom Fields" plugin. I've looked around, but haven't found anything that works.

What I have found should work, but I'm having trouble implementing it. Any help is much appreciated.

Suggestions?

<?php
/*
Template Name: Case Page
*/
get_header(); ?>

<?php
$args = array (
    'post_type' => "case-page");
$the_query = new WP_Query( $args ); ?>

<?php 
      $temp = $wp_query; 
      $wp_query = null; 
      $wp_query = new WP_Query(); 
      $wp_query->query('showposts=6&post_type=news'.'&paged='.$paged); 

      while ( $the_query->have_posts() ) : $the_query->the_post(); 
    ?>
<div class="container"> 
    <div class="row">
        <div class="span10 offset1">    
            <h2><?php the_field( 'case_number' ); ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h2>
            <p><strong>By: </strong> <?php the_field( 'by' ); ?></p>
            <?php the_excerpt(); ?>
            <p><small>( Updated:  <?php the_field( 'updated_date' ); ?> )</small></p>
        </div> <!-- end span10 -->
    </div> <!-- end row -->
</div> <!-- end container -->


<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

3 Answers

I'm very confused on what you're trying to do here. Why are you setting $wp_query to null and then to $temp which holds... the default query? I don't get it. If you need to reset the query to default, there's a wp_reset_query() or wp_reset_postdata() function you can use depending on your needs.

When you say it "doesn't work" what do you mean exactly? What's the output between <nav>? Try dumping the output of $wp_query() at the point of that nav so you can see what's going on at that point. Post that here.

You also need to make that $wp_query object a global if you want to do stuff with it.

Troy A.
Troy A.
13,069 Points

Hi Paul, Thanks for your reply...

I'm trying to add pagination to this custom template. Here's the code for the page:

<?php

/*

Template Name: Case Page

*/

 get_header(); ?>

<?php
$args = array (
    'post_type' => "case-page"
);

$the_query = new WP_Query( $args );

?>


<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="container"> 
    <div class="row">
        <div class="span10 offset1">    
            <h2><?php the_field( 'case_number' ); ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?>            </a></h2>

            <p><strong>by: </strong> <?php the_field( 'by' ); ?></p>
            <?php the_excerpt(); ?>
            <p><small>( Posted:  <?php the_field( 'date_added' ); ?> )</small></p>

        </div> <!-- end span10 -->
    </div> <!-- end row -->
</div> <!-- end container -->


<?php endwhile; else: ?>

<p>There are no posts or pages here</p>

<?php endif; ?>

<?php get_template_part( 'content', "cta"); ?>
<?php get_footer(); ?>

The confusing code I posted about was found online, but I was totally lost how to integrate it. Sorry for the confusion. This is my first attempt at building a wordpress theme.... How would I add pagination to this page?

Check out the documentation on http://codex.wordpress.org/Function_Reference/next_posts_link

You had the pagination at the right point, I just think the query madness in your original template is messing it up. The next_posts_link() is just pulling straight from the current query (on in most cases, the default query), so as long as you're in an object with multiple items, you should be good, it should be paginating SOMETHING (maybe not the right thing, but something).

That's why I want to see the output in <nav>, to see what exactly it's outputting, if anything. If it's outputting nothing, that means you're not in a valid query or you're only returning one item.

Make sure you're in the query you want to paginate before calling those _posts_link() functions. You can do this by using echo var_dump to display the contents of the $the_query object. You should be able to tell from that whether you're retrieving what you want or not.

Also just found this http://codex.wordpress.org/Next_and_Previous_Links while surfing through the Codex. It covers some customized next and previous links. I'm not sure if it will help but it sure has a lot of information on how to do these.

Troy A.
Troy A.
13,069 Points

Thanks Paul !!!

It's starting to make sense, but I'm still not putting it together.

I usually get a white page, just header and footer or an "older post link" but it doesn't link to page 2.

This code outputs a header and footer:

<?php

/*

Template Name: Case Page

*/

 get_header(); ?>

<?php
$args = array (
    'post_type' => "case-page"
);

$the_query = new WP_Query( $args );

?>

    <?php
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    // the query
    $the_query = new WP_Query( 'cat=8&paged=' . $paged ); 
    ?>

    <?php if ( have_posts() ) : ?>

    <?php
    // the loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>


<div class="container"> 
    <div class="row">
        <div class="span10 offset1">    
            <h2><?php the_field( 'case_number' ); ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h2>

            <p><strong>by: </strong> <?php the_field( 'by' ); ?></p>
            <?php the_excerpt(); ?>
            <p><small>( Posted:  <?php the_field( 'date_added' ); ?> )</small></p>

        </div> <!-- end span10 -->
    </div> <!-- end row -->
</div> <!-- end container -->


<?php endwhile; ?>

<?php
// usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after our query
wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<?php get_template_part( 'content', "cta"); ?>
<?php get_footer(); ?>

When you say "blank page" is it outputting any code at all between the header and footer? It is displaying anything from the content-cta template at all (assuming this template part worked before, of course)?

Troy A.
Troy A.
13,069 Points

Yeah, the template worked before...just without pagination.

The code above displays the header, footer and content-cta template, but nothing in the container div.

So the query you're trying to make is somehow invalid, that has to be it. If you're not seeing the container at all, the loop is not running. Go ahead and do a var_dump of the $the_query variable inside the container. This will at least show you which object is being picked up.

The default var_dump is very hard to read but you can prettify it by using print_r(). http://stackoverflow.com/questions/1168175/is-there-a-pretty-print-for-php

SORRY SORRY, not inside the container, you have to do it outside the loop. var_dump before your while have_posts() loop.

Troy A.
Troy A.
13,069 Points

I have no idea what I'm doing with this :D not sure I did the var_dump correctly... it says: array(1) { ["post_type"]=> string(10) "case-page" }

So now what you know is that when you parse $the_query, you're not actually getting a valid WP object in return. This means your WP_Query call is not working. This immediately makes me think that you don't have "case-page" content type registered correctly. ACF does not handle content types, only meta fields, so can you post the code in functions.php where you registered the new content type?

(oh and if you didn't, you need to do this)

So now what you know is that when you parse $the_query, you're not actually getting a valid WP object in return. This means your WP_Query call is not working. This immediately makes me think that you don't have "case-page" content type registered correctly. ACF does not handle content types, only meta fields, so can you post the code in functions.php where you registered the new content type?

(oh and if you didn't, you need to do this)

Troy A.
Troy A.
13,069 Points

Oh!!! Maybe that's the issue.... I didn't know I needed to add something to the functions.php. What needs to be added?

Thanks for all your help Paul !!!

Yeah custom post types don't come for free, you have to define them. Read this to start. You'll know it's working when var_dump tosses actual database stuff back at you.

http://codex.wordpress.org/Post_Types http://codex.wordpress.org/Function_Reference/register_post_type

Troy A.
Troy A.
13,069 Points

I'm reading it now.... thanks again for pointing me in the right direction. Cheers!!!

By the way, I love ACF and use it quite frequently to manage metadata, but I don't use their API even though it's quite nice because you can also query it through get_post_meta exactly like any other manually defined meta. This enables you (if you wanted) to rip out ACF later and not affect the site at all. In fact, ACF has a built-in export to allow you to set up native meta from all the stuff you set up in ACF. It really is one of the best plugins out there for developers.

There are also plugins out there to help you with custom types. Pippin Williamson has a nice plugin that handles it in the native way (http://pippinsplugins.com/easy-content-types/) and there's also the Pods Framework (http://pods.io/) but I'd only recommend using a framework if you have many or very complex data types as you're then relying on more non-native code. It seems like right now you only have one that's fairly simple, so the best thing to do would be to manually define it.

Troy A.
Troy A.
13,069 Points

Good to know. Thanks!

You're right, I'm working on very simple data.... that's why figuring out this pagination issue will be huge milestone.

Troy A.
Troy A.
13,069 Points

After a few hours of searching, it ended up being much easier than it seemed.

Biggest lesson - If you're going to use the "Advanced Custom Fields" plugin, you'll need to use different page names than the custom post types you've created.

Here's the code I found on css-tricks.com : (http://css-tricks.com/snippets/wordpress/paginate-custom-post-types/)

It's actually my original code I started this thread with but I removed: <?php $args = array ( 'post_type' => "case-page"); $the_query = new WP_Query( $args ); ?>

and added my custom post type name, an if ( have_posts() ) and used custom pagination that I styled.