是否在模板页面中列出所有原生帖子?

时间:2014-04-24 作者:bestprogrammerintheworld

我已经创建了一个页面模板,我想做一个非常简单的任务:列出所有(本地)帖子

我在模板文件中有以下代码:

$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, 但是我在返回的对象中发现了一些错误,这表明它的post类型不正确(如果我理解正确的话)。它也不会进入WHILE循环。未返回标题。

这是的输出$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
或与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;
我做错了什么?

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

您的查询中没有错误,但有一些代码(插件?)从结果中删除术语id为1或4的类别中的帖子。(通常,术语id为1的类别为“未分类”)

您可以注意到,查看执行的请求

AND ( wp_posts.ID NOT IN (
  SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (1,4)
)
和在\'category__not_in\' 查询变量:

\'category__not_in\' => 
    array (size=2)
      0 => int 4
      1 => int 1
现在看来category__not_in 使用添加参数pre_get_posts 行动

如果我的猜测是正确的,您可以尝试从该挂钩中删除所有操作,以防止任何查询修改:

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...

结束

相关推荐

Query posts by content lenght

我使用下面的代码每6小时随机发布一篇我的网站帖子。if ( false === ( $social_trans_post_id = get_transient( \'$social_trans_post_id\' ) ) ) { $args = array(\'numberposts\' => 1, \'orderby\' => \'rand\'); $social_trans = get_posts($args)