Skip to main content
2 of 3
Added error text and code explanation
EpF
  • 121
  • 1
  • 1
  • 7

new WP_Query() - what is the earliest valid hook?

I'm getting an error from BuddyPress that seems to be being triggered by creating a new WP_Query() during the plugins_loaded action. The query runs fine and does what I want, but I'd like to know if I'm doing it wrong, or if BP is in error here.

Alternatively, is there a parameter I can pass to the query to tell it not to try to retrieve user info? I couldn't see anything in the documentation for the class, but then the no_found_rows parameter isn't documented there either.

The BP error is:

Notice: bp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init()

My plugin includes a class that includes the following code in the __construct() method:

add_action('plugins_loaded', array($this, 'getDocs'));

This is the getDocs() method:

public function getDocs()
{
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'document',
        'no_found_rows' => true,
        'fields' => 'ids',
    );
    $docs = new WP_Query($args);
    wp_reset_query();
    $this->docs = $docs->posts;
}
EpF
  • 121
  • 1
  • 1
  • 7