Sort WordPress Posts With Multiple Criteria

[ad_1]

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:

[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads