I tried to run a query inside a class

this was the structure

    namespace NCK{

    class Runquery{
      public function execute(){
      $query= new WP_Query(array(..............));
      return $query;

But when i used

$getvalue = new NCK\Runquery();

It throws an error

Fatal error: Uncaught Error: Class 'NCK\WP_Query' not found in

is there any way i can make it works?

1 Answer 1


You need to add a \ in front of the WP_Query call to tell PHP that it's in the global namespace and not the namespace of this class.

Try this: $query = new \WP_Query($args);

  • How would you use wp_reset_postdata() afterwards? It appears that it doesn't see the query that way. Nov 19, 2019 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy