Creating a query that get all posts but places meta items first

Question

I have a query that gets jobs based on a category. Some of these jobs are “featured” issue is the old system used a “featured” meta key, the new system uses “job_featured” meta key (both have 1 for true). I need place the the posts with a true meta value ahead of all posts all order by date so this:

post
post
featured
post
job_featured

would output as:

featured
job_featured
post
post
post

I have this to build my query:

      $jobs = array( 
        'post_type' => 'job', 
        'tax_query' => array( array (
          'taxonomy' => 'jcategory',
          'field' => 'slug',
          'terms' => $category
        )),
        'order' => 'DSC',
        'posts_per_page' => get_option('posts_per_page'),
        'paged' => $paged,
      );
...
      $jobs['meta_query'] = array(
        'relation' => 'OR',
        array(
          'key' => 'job_featured',
          'value' => 1
        ),
        array(
          'key' => 'featured',
          'value' => 1
        ),
        array(
          'key' => 'job_featured',
          'value' => 0,
        ),
        array(
          'key' => 'featured',
          'value' => 0,
        )
      );

      $jobs['orderby'] = array(
        'date' => 'DESC',
        'job_featured' => 'DESC',
        'featured' => 'DESC'
      );
$jobs = (object)$jobs;
$jobSearch = new WP_Query($jobs);

the 'value' => 0 is to get all non-featured jobs.

This sorts by date no issue but the feature and job_featured posts are mixed into the results.

Am I missing anything to get this to sort as expected (sort by date then by featured/job_featured)?

0
dcp3450 5 months 2022-03-14T00:43:16-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse