0

My goal here is to query posts by a meta_key, with the values falling in between 1-100. When pulled, it is suppose to be ordered by ascending starting from 1 to 100.

$post = array(
    'posts_per_page' => 10,
    'post_type' => 'post',
    'tax_query' => array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => 'category-1'
    ),
    'meta_query' => array(
        'meta_key' => 'custom-key', 
        'meta_value' => array(1,100),
        'meta_compare' => 'BETWEEN'
    ),
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
);

When looking at the page, it works of getting posts w/ meta-values '1-100', but it is not ordering correctly. It looks random (i.e. 1, 21, 5, 20, 6, 7, 8 , 3, 9, 19) saying in this order even when the page is constantly refreshed.

Any help will be appreciated.

Thanks!

1 Answer 1

2

Researching more, I got it to work with the following code

$post = array( 
    'posts_per_page' => 10,
    'post_type' => 'post',
    'tax_query' => array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => 'category-1'
    ),
    'meta_query' => array(
        array(
            'key' => 'custom-key',
            'value' => array(1,100),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        )
    ),
    'meta_key' => 'custom-key',
    'orderby' => 'meta_value',
    'order' => 'ASC' 
);

Reference: http://www.wphub.com/use-meta_query-query-posts-postmeta/

Your Answer

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

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