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
      //Displaying latest post per author on front page
  function filter_where($where = '') {
    global $wpdb;
    $where .= " AND vipwp_posts.id = (select id from {$wpdb->prefix}posts p2 where p2.post_status = 'publish' and p2.post_author = {$wpdb->prefix}posts.post_author order by p2.post_date desc limit 0,1)";
    return $where;
  add_filter('posts_where', 'filter_where'); 

I would like to modify the code above: Is it possible change the order of the record from date by meta_value?

Example: CHANGE

order by p2.post_date


meta_key='name_key' ORDER BY meta_value

Thanks, but if I remove post_where do not Displaying latest post per author

share|improve this question

Take a look at the WordPress Codex: http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby

function edit_posts_orderby($orderby_statement) {
    $orderby_statement = "meta_value DESC";
    return $orderby_statement;
} add_filter('posts_orderby', 'edit_posts_orderby');

If you're making the WP_Query yourself you can change the orderby parameter to meta_value

Take a look here: http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
Thanks, buy if I remove post_where do not Displaying latest post per author – Marco Oct 20 '12 at 11:56

I think you can do this entirely from the pre_get_posts action. So try removing your posts_where filter and replacing it with:

function wpa_69199( $query ) {
        set_query_var( 'orderby', 'meta_value' );
        set_query_var( 'meta_key', 'name_key' ); //field you'd like to order by
add_action( 'pre_get_posts', 'wpa_69199' );

Note: there is no conditional logic here and all posts everywhere will be sorted this way.

share|improve this answer

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.