geminilabs

Untitled

Apr 8th, 2021 (edited)
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. // This is the meta query
  2.  
  3. $query = new WP_Query([
  4. 'meta_query' => [
  5. 'relation' => 'OR',
  6. ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first!
  7. ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
  8. ],
  9. 'order' => 'DESC',
  10. 'orderby' => 'meta_value_num',
  11. 'post_status' => 'publish',
  12. 'post_type' => 'page', // change this as needed
  13. 'posts_per_page' => 10, // change this as needed
  14. ]);
  15.  
  16. // This is the meta query SQL result (notice the OR)
  17.  
  18. SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
  19. FROM wp_posts
  20. LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_glsr_ranking' )
  21. LEFT JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
  22. WHERE 1=1
  23. AND ( wp_postmeta.post_id IS NULL OR mt1.meta_key = '_glsr_ranking' )
  24. AND wp_posts.post_type = 'page'
  25. AND ((wp_posts.post_status = 'publish'))
  26. GROUP BY wp_posts.ID
  27. ORDER BY wp_postmeta.meta_value+0 DESC
  28. LIMIT 0, 10
Add Comment
Please, Sign In to add comment