[wp-hackers] Pagination for custom loops

Bill Erickson bill.erickson at gmail.com
Thu Feb 21 22:44:01 UTC 2013


If I'm doing a custom query that requires pagination, I try my best to make
it the main query on the page (customize with pre_get_posts). But sometimes
you just have to override $wp_query to paginate a custom query.

global $wp_query;
$wp_query = new WP_Query( $args );
...
wp_reset_query();


---
Bill Erickson
WordPress Consultant
http://www.billerickson.net

NOTE: To better serve my clients, partners, and most importantly my family,
I've begun checking email only three times per day.

For phone calls, please schedule here: *http://meetme.so/billerickson*. If
it's an emergency, please call now and leave a voicemail


On Thu, Feb 21, 2013 at 4:37 PM, Becky Resler <becky.absolute at gmail.com>wrote:

> I've done the following in several projects ...
>
> $custom_query = new WP_Query($query_args);
>
> if($custom_query->have_posts()**)
> {
>     // set up the pagination links
>     $pagination = array(
>                     'base'      => home_url().'/page-slug/%_%', //or:
> @add_query_arg("page","%#%"),
>                     'format'    => 'page/%#%', // %#% is replaced by the
> actual page number
>                     'total'     => $custom_query->max_num_pages,
>                     'current'   => $page,
>                     'show_all'  => false,
>                     'end_size'  => 1,
>                     'mid_size'  => 2,
>                     'type'      => 'plain'
>                 );
>
>     // get the pagination links
>     $pagination_links = paginate_links($pagination);
>
>     // display the pagination links above the listing
>     $html .= '<div id="pagination-top" class="pagination">'.$**
> pagination_links.'</div>';
>
>     while($custom_query->have_**posts())
>     {
>         $custom_query->the_post();
>
>         // put together the html for the post
>     }
>
>     // display the pagination links below the listing
>     $html .= '<div id="pagination-bottom" class="pagination">'.$**
> pagination_links.'</div>';
> }
>
> Becky Resler
> Web Programmer
> Absolute Marketing Group
>
>
>
> On 2/21/2013 4:25 PM, Kirk Wight wrote:
>
>> How do people handle pagination for custom loops (new WP_Query instances,
>> not $wp_query)?
>>
>> It would be great to use the same function calls as the main loop
>> (eg. get_previous_posts_link()), but these all rely on $wp_query. I've
>> been
>> hi-jacking $wp_query and resetting it with wp_reset_query() afterwards,
>> but
>> then I just feel like I've used query_posts, and become sad.
>>
>> Am I missing an obvious solution, or is there a better way to my own
>> method
>> short of writing all my own navigation calls?
>> ______________________________**_________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.**com <wp-hackers at lists.automattic.com>
>> http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
>>
>>
> ______________________________**_________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.**com <wp-hackers at lists.automattic.com>
> http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
>


More information about the wp-hackers mailing list