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.

this is the query that returns the listing of a custom post type

$apartments = $wpdb->get_col( "
                SELECT DISTINCT( post_id ) 
                FROM $wpdb->postmeta 
                WHERE meta_key IN( $fields_string ) AND 
                      post_id IN ( SELECT ID FROM $wpdb->posts 
                      WHERE post_type = 'tvr_apartment' AND 
                            post_status = 'publish' ) 
                )" );

By default this returns the newest first, I want to order by random; this is how i am trying but then there are no results

$apartments = $wpdb->get_col( "
                SELECT DISTINCT( post_id ) 
                FROM $wpdb->postmeta 
                WHERE meta_key IN( $fields_string ) AND 
                      post_id IN ( SELECT ID FROM $wpdb->posts 
                      WHERE post_type = 'tvr_apartment' AND 
                            post_status = 'publish' ) 
                )
                ORDER BY $wpdb->postmeta.post_id rand()" );

what am i doing wrong?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You don't want ORDER BY $wpdb->postmeta.post_id rand(). If you had debugging enabled, you'd see an error. You need to separate ORDER BY values with a comma-- ORDER BY $wpdb->postmeta.post_id, rand(). In your case, that won't do any good because the first value gets precedence, so the query will order by the ID and then randomize "within" that first level order... but there is not "within" in this case.

Just use ORDER BY rand()

share|improve this answer
    
thanks! i will try! –  Toni Michel Caubet Jul 22 '13 at 10:00

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.