WP_Query未按标题排序

时间:2018-07-04 11:05:54

标签: wordpress

我尝试按标题升序获取所有帖子(具有特定的自定义类型),但生成SQL查询,在post_date之后进行排序。 代码:

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'posts_per_page' => -1,
      'orderby' => 'title',
      'order' => 'ASC',
    ) 
  );

while ($my_query->have_posts()) {
    $my_query->the_post();
    // get post excerpt
    get_template_part('content/'.get_post_type());
    // wikiwp_get_post_excerpt($post);
}

查询:

  

选择wp_posts。*从wp_posts,其中1 = 1和wp_posts.post_type =   '成员'AND(wp_posts.post_status ='发布'或wp_posts.post_status   ='已关闭'或wp_posts.post_status ='私有'或wp_posts.post_status ='隐藏')ORDER BY wp_posts.post_date ASC

我真的不明白为什么会这样。我停用了大多数插件,并尝试了不同的组合,但没有成功。

$ my_query:

  

WP_Query对象([查询] =>数组([post_type] =>成员   [posts_per_page] => -1 [orderby] =>标题[order] => ASC)   [query_vars] =>数组([post_type] =>成员[posts_per_page] => -1   [orderby] =>订单[order] => ASC [错误] => [m] => [p] => 0   [post_parent] => [subpost] => [subpost_id] => [attachment] =>   [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] =>   0 [秒] => [分钟] => [小时] => [天] => 0 [月数] => 0 [年]   => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [分页] => 0 [meta_key]   => [meta_value] => [preview] => [s] => [句子] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array()   [category__not_in] =>数组()[category__and] =>数组()   [post__in] =>数组()[post__not_in] =>数组()[post_name__in] =>   Array()[tag__in] => Array()[tag__not_in] => Array()[tag__and]   =>数组()[tag_slug__in] =>数组()[tag_slug__and] =>数组()[post_parent__in] =>数组()[post_parent__not_in] =>数组()   [author__in] =>数组()[author__not_in] =>数组()   [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1   [update_post_term_cache] => 1 [lazy_load_term_meta] => 1   [update_post_meta_cache] => 1 [nopaging] => 1 [comments_per_page] =>   50 [no_found_rows] =>)[tax_query] => WP_Tax_Query对象([查询]   => Array()[relation] => AND [table_aliases:protected] => Array()[queried_terms] => Array()[primary_table] => wp_posts   [primary_id_column] => ID)[meta_query] => WP_Meta_Query对象(   [查询] =>数组()[关系] => [元表] => [元ID栏]   => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array()[clauses:protected] => Array()[has_or_relation:protected] =>)[date_query] => [request] =>选择   wp_posts。* FROM wp_posts在1 = 1且wp_posts.post_type ='member'   AND(wp_posts.post_status ='发布'或wp_posts.post_status =   'closed'或wp_posts.post_status ='private'或wp_posts.post_status =   'hidden')ORDER BY wp_posts.post_date ASC))

3 个答案:

答案 0 :(得分:0)

请使用以下代码:

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'title',
      'order' => 'ASC'
    ) 
  );

您在,之后最后添加了ASC,所以我认为是问题所在。然后$my_query是您的数组所在的位置,因此请像下面那样循环遍历以获取结果想要的。

while($my_query->have_posts()) : $my_query->the_post();
//Your post code here to get post attributes
endwhile;wp_reset_query(); 

答案 1 :(得分:0)

您的代码看起来正确。我想看看您是否在其他地方有任何代码,例如过滤器会更改您创建的查询的输出。听起来您可能有这样的事情,因为您的发布循环正确。

$args = array(
    'post_type' => 'member',
    'posts_per_page' => '-1',
    'order' => 'ASC',
    'orderby' => 'title',
);

$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        get_template_part('content/'.get_post_type());
    }
}

wp_reset_postdata();

答案 2 :(得分:0)

我面临着同样的问题。我使用“ post_title”而不是“ title”解决了该问题。

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'post_title',
      'order' => 'ASC'
    ) 
  );