Class 'Roots\Sage\WP_Query' not found

I’m new to Sage but I have worked with roots for many years.

I have a base-template-home (a copy based on base.php) and inside I have a custom query:

    <?php $args = array('post_type' => 'featured', 'posts_per_page' => 1, 'post_status' => 'publish');
            $featured = new WP_Query($args); 
            while($featured->have_posts()) : $featured->the_post(); 
                the_post_thumbnail(); 
            endwhile; ?>

The problem is that the following error appears:

Fatal error: Class 'Roots\Sage\WP_Query' not found

Probably this question is stupid but I’m a little lost.

Thank you in advance.

Use new \WP_Query

http://php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.shouldicare

2 Likes

What ben said, alternatively you can put at the top of your template

use WP_Query;
3 Likes

Thank you Ben and Kalen!

Putting use WP_Query; at the top of my template page isn’t working. I had it prior to

namespace Roots\Sage\featured_articles;

Not sure what is wrong there, but Ben’s method is working.

You wouldn’t need to use use WP_Query; in a template file if there is no namespace declared in that file. If there is no namespace declared in a PHP file, then it’s in the global namespace, and in this instance so is WP_Query.

You would also never put use before namespace.

1 Like