Same basic WP_Query works on page but not post or custom post type

39 views Asked by At
$applicationQuery = new WP_Query(array( 'post_type' => 'page', 'post_status' => 'any' ));

if($applicationQuery->have_posts()){
  $header_text = "Got post (GOOD)";
}
else{
    $header_text = "No post (BAD)";
}

This same query, if I use page for post_type there are results in the query, but if I change post_type to post or my own custom_post_type there's no result. Why? How can I fix this and query my custom post type?

2

There are 2 answers

0
AVAVT On BEST ANSWER

I fixed the problem by adding 'post_status' => 'published' into the query array.

Edit: The root cause was because the custom posts were programmatically created (not via admin dashboard), and the post_status of those posts were set to published.

Wordpress doesn't know published, by default it only recognize publish. The any value in the query doesn't actually look for "any" post_status, but "any recognized" post_status.

0
Vel On

Try this code.

        $applicationQuery  = query_posts( array( 'post_type' => array('page') ) );

         while (have_posts()) : the_post();       
                //echo the_title();
         endwhile;