0

What would be the disadvantages/problems of overriding a global variable, in this case the $wp_query?

Example in template archive-books.php:

get_header();
global $wp_query;

$wp_query = new WP_Query( 'post_per_page=6&post_type=books' );
6
  • 1
    Why would you want to? On the books archive page that should already be the query, basically. Maybe you are needing to change the post limits?
    – s_ha_dum
    May 22, 2015 at 16:49
  • I'm having pagination problems. The pagination functions gets its query info from the main query and so I would like to know the disadvantages of overriding it vs sending a custom query object to the pagination function.
    – pmqa
    May 22, 2015 at 17:08
  • See the link in my answer. May 22, 2015 at 17:08
  • One more thing, maybe just for the future, but generally custom loops are seldom needed, especially for archives. May 22, 2015 at 17:14
  • You might find this helpful May 23, 2015 at 7:25

1 Answer 1

1

You are interfering with the main query/loop, which is bad - there is nothing more to say.

You can do a custom loop, see the codex article The Loop to start with, or use the pre_get_posts hook, if you want to do something other then the usual behaviour.

If you need to toy with $wp_query, for example to fix the pagination on custom loops, then restore it afterwards.

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.