Sohrab Taee
Sohrab Taee

Reputation: 760

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.

featured field is a true/false type and priority field is a number, and they are created by ACF plugin.

here is my code, but it's not working...

$args = array(
    'post_type' => 'tour',
    'posts_per_page' => 8,
    'orderby'   => 'meta_value_num date',
    'meta_key'  => 'priority',
    'meta_query' => array(
        array(
            'key'     => 'featured_tour',
            'value'   => true,
            'compare' => '=',
        ),
    ),
);
$query = new WP_Query( $args );

Upvotes: 0

Views: 705

Answers (1)

vrajesh
vrajesh

Reputation: 2942

try this :

$args = array(
    'post_type' => 'tour',
    'posts_per_page' => 8,
    'orderby'   => 'meta_value_num',
    'order'=>'DESC',
    'meta_key'  => 'priority',
        'meta_query' => array(
        relation=>'AND',
        array(
            'key'     => 'featured_tour',
            'value'   => true,
            'compare' => '=',
        ),
        array(
            'key'     => 'priority',
            'value'   => array(1,6),  //YOUR VALUES
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ),
    ),
);
$query = new WP_Query( $args );

Upvotes: 1

Related Questions