0
votes

I'm trying to order my WordPress posts by 3 different columns in 2 different tables.

The first is post_date, it's located in wpdb->posts table.

The second is the post's meta key and meta value, located in wpdb->postmeta.

meta_key = et_payment_package
meta_value = 1 or 0

The two columns are in different tables. How do I go about ordering them? I would like to display posts with et_payment_package=1 by their original post dates as the first few posts, followed by every other post with et_payment_package=0.

1
Right now it's just ordered by posts in descending date order. I'm still figuring out how to approach this problem. My guess would be to use an inner join to "append" the meta_key and meta_value columns to the existing posts table, then work on the newly joined table by ordering with the meta_value first, followed by the post_date. Does this sound right? - Procrastinatus
SELECT * FROM wp_posts INNER JOIN wp_postmeta ON wp_postmeta.post_id=wp_posts.ID WHERE post_type='project' AND meta_key='et_payment_package' ORDER BY et_payment_package DESC, post_date DESC; This is the resulting SQL query I've crafted, but I'm unsure how to do this with WP_query. - Procrastinatus

1 Answers

1
votes

In 4.0, you can now pass an array to WP_Query as the value for orderby.

  $query=new WP_Query(array(
            'meta_key'=>'your_meta_key',
            'orderby'=>'date your_meta_key',
            'order'=>'desc'
    ));