Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9283611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:40:31+00:00 2026-06-18T18:40:31+00:00

I have been creating custom WordPress loops with two different approaches, both which involve

  • 0

I have been creating custom WordPress loops with two different approaches, both which involve creating a new instance of the WP_Query object. I typically have multiple loops on a single page.

I do not understand how these two approaches differ, and which is the correct context to use each.

Approach 1: http://codex.wordpress.org/Class_Reference/WP_Query

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        // output
    endwhile;
endif;

wp_reset_postdata();

Approach 2: http://codex.wordpress.org/Function_Reference/wp_reset_postdata

$original_query = $wp_query;
$wp_query = null;

$wp_query = new WP_Query( $args );
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // output
    endwhile;
endif;

$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();

Both appear to yield the same results, however when I turn on WP_DEBUG, I see errors with the second approach such as:

Notice: is_singular was called incorrectly. Conditional query tags do not work before the query is run.

My questions are these:

  • when should I use the $original_query = $wp_query; approach?
  • what is the relevance of storing and restoring $wp_query?
  • why does is return the error message when I use it?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:40:32+00:00Added an answer on June 18, 2026 at 6:40 pm

    Some core functions– some pagination ones, for example (here is an example where this seems to have been a factor)– assume the $wp_query value. If that isn’t set those functions don’t work. You can cheat around this by saving the original $wp_query, running the custom loop, and putting $wp_query back. I would argue that this is rarely the best way to do it, and that if you do think you need to do this chances are you should be using pre_get_posts, or other filters, to alter the main query itself instead of making a new query. I won’t swear that there absolutely are no cases where this would be appropriate, but I do think there is usually a better way.

    Your code does not trigger any notices when I try it but if you did something like this it would:

    $original_query = $wp_query;
    $wp_query = null;
    if (is_single()) {
      echo 'single post';
    }
    $wp_query = new WP_Query( array('posts_per_page' => 5) );
    

    Function like is_single, is_archive, etc. rely on the global $wp_query object, which has been unset. I expect it is in cases like that where you see the notices.

    Most everything you need for reference is in http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php and http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Spring Security. I have been working on creating a custom
I'm relatively new at creating custom content for Sharepoint 2010 and have been having
I have been in both situations: Creating too many custom Exceptions Using too many
I have been creating a peer to peer connection for a new game, that
I'm creating a custom media center wordpress website. I have created my own gallery,
Ok, so I'm familiar with creating local Wordpress builds, and have been chugging along
I have been given a task of creating a common Gridview component which has
I have been creating a custom video player for the web. On some machines
I have been creating a custom username/password validator for a WCF service and ran
I have been working on creating a custom allocator as a fun exercise/practice and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.