0

I am fetching results based on numeric value of post metadata. But I am getting 2 types of results based on whether I'm using 2 or 02 .

Take a look at the images

enter image description here

Here is another

enter image description here

When I am using 02 its okay. But when I am using only 2 its not okay. I am organizing post ids based on meta_value.

Here is the result when I'm using 2

3866
3864
3865
3861
3862

Here is the result when I'm using 02

3866
3864
3861
3862
3865

Why this is not working?

I am using $args['orderby'] = 'meta_value meta_value_num';

1
  • What if you only sort by meta_value_num? Commented May 12, 2018 at 8:46

1 Answer 1

1

If the variable you are storing in meta_value were an integer, you would not be able to store it as '02'. So, apparently, your are storing a string. This indeed gives different orders depending on whether you store '02' or '2', as is described under meta_value in the codex on WP_Query.

So, as the codex says, you must force the string to be converted to integers by ordering with meta_value_num or by additionally specifying meta_type=NUMERIC.

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.