1

With a lot of search and trial&error, I's unable to do the pagination in a custom page template. I's not actually tired, but frustrated, why the most common err issue is not explained in a way so that somebody can dig into the actual problem. In most of the Q&A, there's a solution with a bulk of code, that's not a bullet-proof solution at all. There are lots of philosophy, but there's no actual digging into the solution.

Eventually I came across to Chris' solution to the pagination of a Custom Post Type. I'm not dealing with CPT, but the solution worked for me.

Where, my Query arguments are:

$wp_query->query('posts_per_page=3&post_type=post&order=DESC&orderby=date&post_status=publish'.'&paged='.$paged);

Except that portion everything is as followed as Chris stated. My pagination is working nicely.

But I'm actually concern about some issues:

  1. First, what's the code actually doing?
  2. I think it's now taking more time (performance issue) than the default solution — is that actually right?
  3. Is that safe messing with the global $wp_query?

1 Answer 1

0

This question is opinion based, so the answer can be opinion based too. I assume that, if you need to paginate the posts, you are building a page template to look like an archive page. In this case I totally recommend to use an archive page and to use pre_get_posts action hook for filter the posts if needed.

Why?

Hooking in pre_get_posts action allow you to alter the query for your needs before the main query is executed.

If you let the main query as is, it is executed by Wordpress in order to determine what page to display and then, in the page template, you create a second query to display what you want. It is like leaving Wordpress build all the process and when the content are going to be printed, you say: NO!! Go back and make another query becasue what you have is not what I want to display!! (yes, it is more job to do!!)

Secondary queries and secondary loops can be useful and needed in a several situations, for example in widgets to display latest posts from a certain category, but don't use them if the result of the secondray query/loop are going to be the main content of the page.

Messing with the global $wp_query in a page template can get you to undesired results; At this point the global $wp_query is a reference to a query that was already executed and its values may be used in other parts of the script, so it is not safe messing with it. In the case you need a secondary loop/query, you must use ALWAYS a new WP_Query object and call to wp_reset_postdata() after the secondary loop; or use get_posts() function. Finally, according with the Codex, in a page template the pagination parameteres for a new WP_Query object may need get_query_var('page') instead of get_query_var('paged').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.