1

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?

5

3 Answers 3

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.

1
  • So true! When the 'Post Types Order'-plugin is set to autopilot you can easily forget about it and despair... Thanks a lot, philip!
    – johnrbnz
    Sep 8, 2021 at 15:15
2

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.

1
  • Thank you for the reply. I added the string, however it doesn't work. I tried to deactivate CPO (Custom Post Order) in order to make the default order, without results. Any other ideas?
    – Stefano
    Dec 12, 2016 at 15:13
1

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' ),
);
1
  • This seemed to fix my ordering issue. It's nonsensical that this format works but the default 'order' and 'order_by' args do not. But, c'est la vie. Sep 7, 2021 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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