Sort WordPress Posts With Multiple Criteria

This was a case on a project where a custom listing post type had post meta that specified if it was as platinum sponsor, or a gold sponsor or a verified profile.

The job was to list the platinum sponsors at the top on the archive page, then the gold sponsors, then verified profiles and then rest of the listings.

Here is the sweet query that got it done. You will have to replace the highlighted keys with your own.

<?php 
$q = new WP_Query( array(
'post_type' => 'company',
'tax_query' => array(
  array(
    'taxonomy' => 'service',
    'field'    => 'slug',
    'terms'    => get_query_var( 'service' ),
  ),
),
'paged'   => get_query_var('paged', 1),
'meta_query' => array(
  'relation' => 'AND',
  'platinum_sponsors' => array(
    'key' => 'statusplatinumsponsor_75945',
    'compare' => 'EXISTS',
  ),
  'gold_sponsors' => array(
    'key' => 'statusgoldsponsor_95272',
    'compare' => 'EXISTS',
  ),
  'verified_profiles' => array(
    'key' => 'statusverified_13141',
    'compare' => 'EXISTS',
  ),
),
'orderby' => array( 
  'platinum_sponsors' => 'DESC',
  'gold_sponsors' => 'DESC',
  'verified_profiles' => 'DESC',
),
) ); ?>

Refer these:

Hello, I am Arun Basil Lal. Thank you for reading!

I am a freelance WordPress developer with over a decade of experience. I am passionate about making things and traveling the world.

Divi WordPress Theme - My Review

Divi WordPress Theme
Divi is a WordPress theme that web designers do not want you to know. It comes with a drag-and-drop theme builder. You can build beautiful looking unique websites without touching a line of code. Just choose from one of the many pre-made layouts, or pick elements and arrange them any way you like.

Divi is every WordPress developer's wet dream. Surprise your clients with neat responsive websites and have fun building them.

Divi comes from Elegant Themes. If you enjoy building websites, you *need* an Elegant Themes membership. 87 beautiful themes and 5 plugins for the cost of less than a candy-bar each!


Note: I am an avid user of Divi myself and this is a honest review. I wouldn't recommend something that I do not personally find amazing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Share With Friends

Email Stamp

Free Newsletter

Get my newest blog posts and insider tips. No more than a few emails a year.

Just enter your email address below and click Sign Up Now

Please enter all required fields
Correct invalid entries

You have Successfully Subscribed!