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

order - Wp query orderby 'title' doesn't work

programmeradmin0浏览0评论

I would like to order a list of post title alphabetically. I am using this specific query

// WP_Query arguments
$args = array(
    'category_name'     => 'reportage',
    'order'             => 'ASC',
    'orderby'           => 'title',
);

// The Query
$query = new WP_Query($args);

// The Loop
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo '<ul><li style="float:left; width:100%;"><a href="'.get_the_permalink().'" style="color:#D34D3D">'.get_the_title().'</a></li></ul>';
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

The query worked perfect until a certain point of the list where the order seems to be messed up.

Why the order doesn't work correctly for all the post title?

I would like to order a list of post title alphabetically. I am using this specific query

// WP_Query arguments
$args = array(
    'category_name'     => 'reportage',
    'order'             => 'ASC',
    'orderby'           => 'title',
);

// The Query
$query = new WP_Query($args);

// The Loop
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo '<ul><li style="float:left; width:100%;"><a href="'.get_the_permalink().'" style="color:#D34D3D">'.get_the_title().'</a></li></ul>';
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

The query worked perfect until a certain point of the list where the order seems to be messed up.

Why the order doesn't work correctly for all the post title?

Share Improve this question edited Dec 12, 2016 at 23:44 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Dec 12, 2016 at 13:11 StefanoStefano 1391 gold badge3 silver badges13 bronze badges 5
  • What do you mean by doesn't work correctly? Do you have an example? Wrong db collation? – birgire Commented Dec 12, 2016 at 13:29
  • Sure, you can see the example on this page: camillabaresani/dev/i-miei-articoli/interviste/… – Stefano Commented Dec 12, 2016 at 13:30
  • most likely it is working properly.... without knowing what is your data set it is impossible to say anything more intelligent. – Mark Kaplun Commented Dec 12, 2016 at 13:39
  • show this link wordpress.stackexchange/questions/54347/… – Tuychiev Toir Commented Jan 16, 2018 at 12:07
  • In your link "Sommario" is perfect display title in ASC orde then what is the issue? – Jignesh Patel Commented Jan 16, 2018 at 12:14
Add a comment  | 

3 Answers 3

Reset to default 4

I came across the same problem and could not figure it out. Eventually found the problem when I took a look at the custom post type entries in the database and noticed that the results that were out of order had values set in the menu_order column. After a bit of digging I realised that it was the Post Types Order plugin that was set to auto and 'injecting' an additional parameter to the orderby parameter. I understand you can turn off the auto function in the plugin settings but I opted to do it via the query parameters:

$loop = new WP_Query(
    array(
    'post_type' => 'my-custom-post-type', 
    'order' => 'ASC', 
    'orderby' => 'title',
    'ignore_custom_sort' => true
    )  
); 

And BINGO! the results came back in the correct order. Hope this helps someone.

Try adding 'suppress_filters' => true to your query args. Maybe you have a plugin that modifies the queries.

You could also check if the posts out of order (the first one) is a sticky post. Sticky posts by default skip the order queue.

You can disable this by adding 'ignore_sticky_posts' => true to your query args.

Problem is $args, do this:

$args = array(
    'category_name'     => 'reportage',
    'orderby' => array( 'title' => 'ASC' )
);

You can also order by multiple parameters, like:

$args = array(
    'category_name'     => 'reportage',
    'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC', 'post_date' => 'DESC' ),
);
发布评论

评论列表(0)

  1. 暂无评论