WP_Query() doesn’t work inside a custom class

What would prevent WP_Query() from working inside a class method?

class MyClass
{
    function __construct()
    {
        $this->myFunc();
    }
    public function myFunc()
    {
        global $post, $wp_query;
        $args_ = array(
              'post_type' => 'post',
              'posts_per_page' => -1,
              'suppress_filters' => false
        );
        $the_query = new WP_Query( $args_ ); // doesn't work
        /*...*/
    }
}

It all works perfectly outside of the class, I can’t find a logical reason as to why it does not work inside.

Read More

Update:
The displayed error is

Fatal error: Call to undefined function is_user_logged_in() in
/home/oricoil/public_html/roofdagan1/wp-includes/query.php on line
3174

Related posts

2 comments

  1. Could it be a conflict with some other variable, declared inside your class?

    EDIT

    The solution (see the comments) was changing

    $this->myFunc();
    

    to

    add_action('init', array($this, 'myFunc'));
    
  2. I met the same problems. My solution is add a back slash and it works now.

    $professor = new WP_Query(array(
               'post_type'=>'professor',
                's'=>sanitize_text_field($data['term'])
            )); 
    

Comments are closed.