WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this function:

  global $wpdb;
  // adjust the query to fit your needs  
  $posts = $wpdb->get_results( $wpdb->prepare(
    "SELECT * FROM $wpdb->posts WHERE
    post_type = 'post' AND post_status = 'publish' AND
    post_title LIKE %s",

How do I set the order => ASC in this query? I want to show my posts alphabetically...

share|improve this question
You could look at the MySQL docs: dev.mysql.com/doc/refman/5.0/en/select.html As written, this is a pure SQL question and would be off topic per the faq. – s_ha_dum Jul 30 '13 at 14:06
up vote 4 down vote accepted

Why don't you use WordPress get_posts?

You can add the parameter orderby => 'title' as you want.

$args = array( 
    'posts_per_page' => -1, 
    'order'          => 'ASC', 
    'orderby'        => 'title' 

$posts = get_posts( $args );


If you want the sql you can just add ORDER BY post_title:

SELECT * FROM $wpdb->posts 
WHERE post_type = 'post' 
AND post_status = 'publish' 
AND post_title LIKE %s
ORDER BY post_title
share|improve this answer
It is not showing me the posts if I use ORDER BY post_title... When I remove it, it is showing the posts. That's weird. I am not using the first solution, because this is a function where I have to use MySQL. – Johan Jul 30 '13 at 14:18

Your Answer


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.