0

i want to show posts with age between 5 and 10 usind WP_Query.how i can do this?

$args = array(
'post_type'  => 'my_custom_post_type',
'meta_key'   => 'age',
'orderby'    => 'meta_value_num',
'order'      => 'ASC',
'meta_query' => array(
    array(
        'key'     => 'age',
        'value'   => array( 3, 4 ),
        'compare' => 'IN',
    ),
),);$query = new WP_Query( $args );

1 Answer 1

1

You can compare BETWEEN in the meta_query and you need to set the type too for NUMERIC.

For example

$args = array(
'post_type'  => 'my_custom_post_type',
'meta_key'   => 'age',
'orderby'    => 'meta_value_num',
'order'      => 'ASC',
'meta_query' => array(
    array(
        'key'     => 'age',
        'value'   => array( 5, 10 ),
        'compare' => 'BETWEEN',
        'type'    => 'NUMERIC'
    )
));
$query = new WP_Query( $args );

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.