How to fix WP_Query not listing any posts

Problem:

In your custom Wordpress plugin or theme you have code like

wp-query-example.php
$query = new WP_Query( );
while ( $query->have_posts() ) {
    $query->the_post();
    // ... your code to process the post ...
}

But $query->have_posts() is always false and the loop is never executed.

Solution

WP_Query works only if you use an appropriate query. Using no query at all is not equivalent to list all posts!

This is likely what you want to do:

wp-query-fix.php
$query = new WP_Query(array('post_type' => 'post'));

Check out similar posts by category: PHP, Wordpress