Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the meta query
- $query = new WP_Query([
- 'meta_query' => [
- 'relation' => 'OR',
- ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first!
- ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
- ],
- 'order' => 'DESC',
- 'orderby' => 'meta_value_num',
- 'post_status' => 'publish',
- 'post_type' => 'page', // change this as needed
- 'posts_per_page' => 10, // change this as needed
- ]);
- // This is the meta query SQL result (notice the OR)
- SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
- FROM wp_posts
- LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_glsr_ranking' )
- LEFT JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
- WHERE 1=1
- AND ( wp_postmeta.post_id IS NULL OR mt1.meta_key = '_glsr_ranking' )
- AND wp_posts.post_type = 'page'
- AND ((wp_posts.post_status = 'publish'))
- GROUP BY wp_posts.ID
- ORDER BY wp_postmeta.meta_value+0 DESC
- LIMIT 0, 10
Add Comment
Please, Sign In to add comment