Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

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 );
share|improve this answer
    
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
1  
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. –  Rilwis 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. –  Rilwis Sep 5 '12 at 5:03
    
Perfect Rilwis...Thanks :) –  swtshweta Sep 5 '12 at 12:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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