A
A
Andrey Dontsov2018-08-12 15:30:32
WordPress
Andrey Dontsov, 2018-08-12 15:30:32

How to make a selection of WP posts sorted by a custom field?

Good day!
The goal is to implement a "showcase" of records, i.e. displaying them on the page in a certain order, sorted by an arbitrary numeric field "showcase_position" (advanced custom field).
For example, there are 20 records, you need to display only 10, but in a certain order:
the first is the record with the value "1" of the arbitrary field "showcase_position";
the second is a record with a value of "2" of an arbitrary field "showcase_position";
the third - a record with a value of "3" of an arbitrary field "showcase_position";
And for the rest of the 7, this field is not set, empty, but they also need to be displayed, the order is no longer so important, let it be by date or id.
Here are the selection parameters, but here we get records ONLY those with "
And you need to get both filled and empty fields, but sorted by this field.

$args = array(

  'post_type'   => 'services',
  'posts_per_page' => 10,
  'order'       => 'ASC',

  'meta_key'    => 'showcase_position', // ключ поля ACF
  'orderby'     => 'meta_value_num',

  'tax_query' => array(
    'relation' => 'AND',
    array(
      'taxonomy' => 'service__location',
      'field'    => 'id',
      'terms'    => array( $location__id ),
    ),
    array(
      'taxonomy' => 'service__type',
      'field'    => 'id',
      'terms'    => array( $service_type_id ),
    )
  )
);

Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question