Order query by post meta value

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

Read More
order by p2.post_date

TO

meta_key='name_key' ORDER BY meta_value

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

Related posts

Leave a Reply

2 comments

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