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 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

1 Answer 1

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 );

print_r($posts);

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

 
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.