2

I want to display posts sorted by slug on a page. Is it possible to order posts by slug using query_posts or Wp_query?

2

In WordPress the post slug is saved in database with name post_name, so to sort posts by slug just use: orderby=name.

Edit:

Here's the example of query:

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'orderby' => 'name'
);
$query = new WP_Query( $args );
  • You are right Rilwis. But it will only work with custom query and not with Query_posts or Wp_query. – swtshweta Sep 3 '12 at 11:48
  • 2
    What do you mean custom query vs query_posts & WP_Query? If you're using query_posts or WP_Query, that is custom query. And you just need to pass orderby=post_name as a parameter. – Anh Tran Sep 3 '12 at 14:52
  • I tried this too, but it didn't worked for me. Can you give me some example for this? – swtshweta Sep 5 '12 at 4:39
  • Just updated my answer with example. The orderby value should be name instead of post_name. My fault. – Anh Tran Sep 5 '12 at 5:03
  • 1
    And note that WP_Query defaults to 'order' => 'DESC' so you might want to explicitly add 'order' => 'ASC'. – denishaskin May 17 '17 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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