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();
var_dump($getvalue->execute());

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);

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *