最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Order custom post type is beign ignored

programmeradmin0浏览0评论

I have a custom post type "car". I've created a loop to show all the titles of that post type. I want to show them alphabetically. But the order is ignored.

<?php
$loop = new WP_Query( array(
    'post_type' => 'car',
    'orderby' => 'title',
    'order' => 'DESC'
)
);
while ( $loop->have_posts() ) : $loop->the_post();

the_title();

endwhile; wp_reset_query(); 
?>

The order output is the same order of the input. So I've added Volkswagen Tiguan first, so that is shown first and not Audi A1 (for example).

I have a custom post type "car". I've created a loop to show all the titles of that post type. I want to show them alphabetically. But the order is ignored.

<?php
$loop = new WP_Query( array(
    'post_type' => 'car',
    'orderby' => 'title',
    'order' => 'DESC'
)
);
while ( $loop->have_posts() ) : $loop->the_post();

the_title();

endwhile; wp_reset_query(); 
?>

The order output is the same order of the input. So I've added Volkswagen Tiguan first, so that is shown first and not Audi A1 (for example).

Share Improve this question asked Nov 26, 2020 at 20:18 DennisDennis 1358 bronze badges 1
  • 1 order should be ASC if you want them displayed alphabetically. i.e. (1, 2, 3, a, b, c, d) – Den Isahac Commented Nov 27, 2020 at 8:18
Add a comment  | 

1 Answer 1

Reset to default -1

quick info, use this to know what is being return:

$loop = new WP_Query( array(
    'post_type' => 'car',
    'orderby' => 'title',
    'order' => 'asc'
)
);
echo '<pre>';
print_r($loop);

wp_reset_postdata();

Also I believe is better at the end reset the postdata to be sure that nothing is being change in the database. For all I had read this is good to use everytime we open a new WP_Query.

发布评论

评论列表(0)

  1. 暂无评论