1

Please forgive me if my question isn't the clearest and I'm not using this exactly correct... I've never posted before.

I am trying to setup a query on a real estate website that I'm developing that will give precedence to the company's listings (a custom post type) before showing the other listings (of the same custom post type), based on the value of a custom field. Inside of my meta_query array, I can add an array such as the following below to show the office's listings by means of a custom field on the post:

array(
  'key'        => 'office_name',
  'compare'    => 'LIKE',
  'value'      =>  'XYZ Realty'
),

Using the snippet above in my query grabs the office's listings as expected, but only "their" listings (or those posts that have their office name in the custom field).

For sorting purposes, I am using the following to sort by price, which is another custom field on my custom post type ("listing"):

'meta_key' => 'price', 
'orderby' => 'meta_value_num',
'order'  => 'DESC'

Which also works as intended. So with that... is it possible to tweak the query so that it will give the office's listings a priority or precedence to be shown first and then those that are not theirs immediately following?

0

2 Answers 2

0

well you meta_query excludes all the non xyz results, so you would have to adapt that part, that it also includes all the other ones.

in the example bellow, the query calls for xyz and for those, who dont have that field set at all. resulting in 3 meta queries, one for price, one for xyz and one for the others. all these are important for the proper sorting. not sure if it works out of the box, but its a start. remember to set your cpt..

$args = array(
  'post_type' => 'your_cpt',
  'meta_query' => array(
    array(
      'relation' => 'AND',
      'price' => array(
        'key' => 'price',
        'compare' => 'EXISTS',
      ),
      array(
        'relation' => 'OR',
        'xyz_realty' => array(
          'key' => 'office_name',
          'compare' => 'LIKE',
          'value' =>  'XYZ Realty'
        ),
        'other_realty' => array(
          'key'       => 'office_name',
          'compare'   => 'NOT EXISTS',
        ),
      )
    ),
  ),
  'orderby' => array(
    'xyz_realty' => 'ASC',
    'other_realty' => 'ASC',
    'price' => 'DESC'
  )
);
1
  • That looks a WHOLE lot more elegant than my final working version! Commented Aug 31, 2018 at 2:22
0

You could do a sort by that key if you have it setup some way that would always show it either first or last.

You could also do a query for that key value, then have a second go for listings that don’t match that meta key value.

1
  • Thanks for the suggestions, Eric. I tried the idea of running a secondary query, but the problem comes in with pagination. Lets say I am only showing 30 posts_per_page in each of the queries, I don't know how to keep the pagination accurate. Commented Aug 30, 2018 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.