0

I've created a page template and I want to do a really simple task: List all (native) posts

I have this code inside the template-file:

$args = array(
        'posts_per_page'   => -1,
        'post_type'        => 'post'
);          
$all_posts_query = new WP_Query($args);
while ($all_posts_query->have_posts()) : $all_posts_query->the_post(); 
echo '*' . get_the_title() . '*';
endwhile;
wp_reset_query();
echo 'DEAD END';
exit;

I want to get all posts, but I get some error in the returned object which tells it's incorrect post-type (IF I understand it correctly). It does NOT go into the WHILE-loop either. No title is returned.

This is the output of $all_posts_query:

object(WP_Query)[2627]
  public 'query' => 
    array (size=2)
      'posts_per_page' => int -1
      'post_type' => string 'post' (length=4)
  public 'query_vars' => 
    array (size=61)
      'posts_per_page' => int -1
      'post_type' => string 'post' (length=4)
      'error' => string '' (length=0)
      'm' => string '' (length=0)
      'p' => int 0
      'post_parent' => string '' (length=0)
      'subpost' => string '' (length=0)
      'subpost_id' => string '' (length=0)
      'attachment' => string '' (length=0)
      'attachment_id' => int 0
      'name' => string '' (length=0)
      'static' => string '' (length=0)
      'pagename' => string '' (length=0)
      'page_id' => int 0
      'second' => string '' (length=0)
      'minute' => string '' (length=0)
      'hour' => string '' (length=0)
      'day' => int 0
      'monthnum' => int 0
      'year' => int 0
      'w' => int 0
      'category_name' => string '' (length=0)
      'tag' => string '' (length=0)
      'cat' => string '' (length=0)
      'tag_id' => string '' (length=0)
      'author' => string '' (length=0)
      'author_name' => string '' (length=0)
      'feed' => string '' (length=0)
      'tb' => string '' (length=0)
      'paged' => int 0
      'comments_popup' => string '' (length=0)
      'meta_key' => string '' (length=0)
      'meta_value' => string '' (length=0)
      'preview' => string '' (length=0)
      's' => string '' (length=0)
      'sentence' => string '' (length=0)
      'fields' => string '' (length=0)
      'menu_order' => string '' (length=0)
      'category__in' => 
        array (size=0)
          empty
      'category__not_in' => 
        array (size=2)
          0 => int 4
          1 => int 1
      'category__and' => 
        array (size=0)
          empty
      'post__in' => 
        array (size=0)
          empty
      'post__not_in' => 
        array (size=0)
          empty
      'tag__in' => 
        array (size=0)
          empty
      'tag__not_in' => 
        array (size=0)
          empty
      'tag__and' => 
        array (size=0)
          empty
      'tag_slug__in' => 
        array (size=0)
          empty
      'tag_slug__and' => 
        array (size=0)
          empty
      'post_parent__in' => 
        array (size=0)
          empty
      'post_parent__not_in' => 
        array (size=0)
          empty
      'author__in' => 
        array (size=0)
          empty
      'author__not_in' => 
        array (size=0)
          empty
      'ignore_sticky_posts' => boolean false
      'suppress_filters' => boolean false
      'cache_results' => boolean true
      'update_post_term_cache' => boolean true
      'update_post_meta_cache' => boolean true
      'nopaging' => boolean true
      'comments_per_page' => string '50' (length=2)
      'no_found_rows' => boolean false
      'order' => string 'DESC' (length=4)
  public 'tax_query' => 
    object(WP_Tax_Query)[2593]
      public 'queries' => 
        array (size=1)
          0 => 
            array (size=5)
              ...
      public 'relation' => string 'AND' (length=3)
  public 'meta_query' => 
    object(WP_Meta_Query)[2594]
      public 'queries' => 
        array (size=0)
          empty
      public 'relation' => null
  public 'date_query' => boolean false
  public 'request' => string 'SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND ( wp_posts.ID NOT IN (

                    SELECT object_id

                    FROM wp_term_relationships

                    WHERE term_taxonomy_id IN (1,4)

                ) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC ' (length=322)
  public 'posts' => 
    array (size=0)
      empty
  public 'post_count' => int 0
  public 'current_post' => int -1
  public 'in_the_loop' => boolean false
  public 'comment_count' => int 0
  public 'current_comment' => int -1
  public 'found_posts' => int 0
  public 'max_num_pages' => int 0
  public 'max_num_comment_pages' => int 0
  public 'is_single' => boolean false
  public 'is_preview' => boolean false
  public 'is_page' => boolean false
  public 'is_archive' => boolean false
  public 'is_date' => boolean false
  public 'is_year' => boolean false
  public 'is_month' => boolean false
  public 'is_day' => boolean false
  public 'is_time' => boolean false
  public 'is_author' => boolean false
  public 'is_category' => boolean false
  public 'is_tag' => boolean false
  public 'is_tax' => boolean false
  public 'is_search' => boolean false
  public 'is_feed' => boolean false
  public 'is_comment_feed' => boolean false
  public 'is_trackback' => boolean false
  public 'is_home' => boolean true
  public 'is_404' => boolean false
  public 'is_comments_popup' => boolean false
  public 'is_paged' => boolean false
  public 'is_admin' => boolean false
  public 'is_attachment' => boolean false
  public 'is_singular' => boolean false
  public 'is_robots' => boolean false
  public 'is_posts_page' => boolean false
  public 'is_post_type_archive' => boolean false
  public 'query_vars_hash' => string '2d0de38f6a4ebb9acf2b3533d94fd229' (length=32)
  public 'query_vars_changed' => boolean true
  public 'thumbnails_cached' => boolean false
  private 'stopwords' => null

boolean true

OR with print_r:

WP_Query Object ( [query] => Array ( [posts_per_page] => -1 [post_type] => post ) [query_vars] => Array ( [posts_per_page] => -1 [post_type] => post [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( ) [category__not_in] => Array ( [0] => 4 [1] => 1 ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [nopaging] => 1 [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 4 [1] => 1 ) [include_children] => [field] => term_id [operator] => NOT IN ) ) [relation] => AND ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => ) [date_query] => [request] => SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (1,4) ) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash] => 2d0de38f6a4ebb9acf2b3533d94fd229 [query_vars_changed] => 1 [thumbnails_cached] => [stopwords:WP_Query:private] => )

When I use this code (custom post type) it works (in the same template)...

$args = array( 'post_type' => 'personnel', 
               'posts_per_page' => -1 
              );
 $all_posts_query = new WP_Query( $args );
while ($all_posts_query->have_posts()) : $all_posts_query->the_post(); 
echo '*' . get_the_title() . '*';
endwhile;
wp_reset_query();
echo 'DEAD END';
exit;

What am I doing wrong?

1

There is nothing wrong in your query, but there is some code (a plugin?) that remove from results the posts in the category having term id 1 or 4. (Normally category with term id 1 is 'uncategorized')

You can notice that looking at request performed

AND ( wp_posts.ID NOT IN (
  SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (1,4)
)

and at 'category__not_in' query var:

'category__not_in' => 
    array (size=2)
      0 => int 4
      1 => int 1

Now its seems that category__not_in argument is added using pre_get_posts action.

If my guess is correct, you can try to remove all actions from that hook to prevent any query modification:

global $wp_filter;
$pgp_hooks = FALSE;
if ( isset( $wp_filter['pre_get_posts'] ) ) {
  // save hooks
  $pgp_hooks = $wp_filter['pre_get_posts']);
  // remove hooks
  unset( $wp_filter['pre_get_posts'] );
}
// set 'suppress_filters' param to true to skip sql filters
$args = array(
  'posts_per_page' => -1, 'post_type' => 'post', 'suppress_filters' => true
);
// query
$all_posts_query = new WP_Query( $args ); 
// reset hooks after query is performed
if ( $pgp_hooks !== FALSE ) {
  $wp_filter['pre_get_posts'] = $pgp_hooks; 
}
// loop goes here...
  • Your qualified was very correct!!! Once again a plugin I think is very useful (Post Types Order) messed things up for me. I get the thing with unsetting pre_get_posts-filter. But I'm not sure what suppress_filter actually does? What "kind" of filters? – bestprogrammerintheworld Apr 24 '14 at 15:54
  • Anyway - It was your code with removing and restoring the pre_get_posts - filter hook that really made my day. THANKS A LOT!!! – bestprogrammerintheworld Apr 24 '14 at 16:00
  • @bestprogrammerintheworld you're welcome. 'suppress_filter' remove all 'posts_*' filters, like 'posts_where', 'posts_join' and so on (there are 19 different 'posts_*' filters iirc) – gmazzap Apr 24 '14 at 17:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.