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?
1 Answer
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– swtshweta2012-09-03 11:48:28 +00:00Commented Sep 3, 2012 at 11:48
-
2What 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_nameas a parameter.Anh Tran– Anh Tran2012-09-03 14:52:16 +00:00Commented Sep 3, 2012 at 14:52 -
I tried this too, but it didn't worked for me. Can you give me some example for this?swtshweta– swtshweta2012-09-05 04:39:21 +00:00Commented Sep 5, 2012 at 4:39
-
Just updated my answer with example. The orderby value should be
nameinstead ofpost_name. My fault.Anh Tran– Anh Tran2012-09-05 05:03:56 +00:00Commented Sep 5, 2012 at 5:03 -
1And note that WP_Query defaults to
'order' => 'DESC'so you might want to explicitly add'order' => 'ASC'.denishaskin– denishaskin2017-05-17 18:30:54 +00:00Commented May 17, 2017 at 18:30