Don’t override query vars with query_posts arguments

Quite often, i find in themes custom queries to customize the content displayed on a page. These themes use either ‘query_posts’ or ‘WP_Query’ to achieve that. For example, the code below allows to exclude posts of some categories from homepage.

if (is_home()) {
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $build_homepage_query = array (
    'paged'=> $paged,
    'category__not_in' => $theme->get_option('homepage_exclude_categories'),
    'posts_per_page' => $theme->get_option('homepage_posts_per_page')
  );
  query_posts($build_homepage_query);
}

The author of this code took care to not override the query var ‘paged’, not to break the pagination. But if there are some other query vars in the initial query, there are not included in the custom query. All is great when the developper makes his tests because, on a fresh WordPress install, there is no other query vars in the inital query for the homepage.

However, in the case of a multilingual website, the query does contain a new query var ‘lang’ to allow the content to be filtered by language. And the custom query above does not contain the query var ‘lang’, so the code above will not play nicely on multilingual websites.

So a better code is proposed below to keep *all* query vars from the initial query (not only the query var ‘paged’ ) and to modify *only* the query vars needed for the customization purpose.

if(is_home()) {
  $build_homepage_query = array (
    'category__not_in' => $theme->get_option('homepage_exclude_categories'),
    'posts_per_page' => $theme->get_option('homepage_posts_per_page')
  );
  $build_homepage_query = array_merge($GLOBALS['wp_query']->query_vars, $build_homepage_query); // don't reset other query_vars!
  query_posts($build_homepage_query);
}

 

2 thoughts on “Don’t override query vars with query_posts arguments

  1. qp

    There’s a problem compatibility with wp ecommerce in admin area.
    If admin area is not in english, wp ecommerce tools remain in english.
    Disabling polylang, wp ecommerce tools are translated according to admin area language choose.

    1. Frédéric Post author

      The forum is a much better place to get support. Just for you to know: I never tested wp-ecommerce and do not plan to support it in near future. However, I am aware of at least one user successfully using wp-ecommerce and Polylang together but I also know that it required several tweaks to make it happen.

Comments are closed.