0
votes

I want to retrive custom post and want to sort it by title. However when i made a dump of what exact SQL request is sent, i found out the orderby is using menu_order instead of title

Here's the code:

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

Heres the dump

"SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'custom_post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC "

Hence when i retrive the custom posts, its not in the order i want it to be. Your help is appreciated

1
What method are you using to query the posts?Spartacus

1 Answers

0
votes

You can create new WP_Query instance to achieve the exact expected result.

<?php
$args = array(
        'post_type' => 'custom_post_type',
        'order'     => 'ASC',
        'orderby'   => 'title',
    );
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <div class="smal-sec">
        <h3><?php the_title();?></h3>
        <?php the_content();?>
    </div>
<?php
endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>

This query will help you to display all posts order by title (A -> Z). Please make sure no such plugins are there, which is actually overwriting the WP_Query instance.