Skip Navigation

[Resolved] WordPress meta query on number comparaison smaller not working?

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by philippeK 5 years, 1 month ago.

Author
Posts
#1582227

I have a custom post field that store number like: 1,2,3 and so on. I use Toolset to generate the custom post and field. I want to display all the custom post that have this number value smaller then let's say 2, here is my query, but WordPress doesn't display the correct post:
$args = array(
'post_type' => 'video',
'posts_per_page' => 1,
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => 'wpcf-number',
'value' => 2,
'meta_compare' => '<',
'type' => 'UNSIGNED'
)
)
);

$latest_interview = new WP_Query($args);

echo $latest_interview->request;
This is how the generated SQL appears:

SELECT wpasdasdasd_posts.ID
FROM wpasdasdasd_posts
INNER JOIN wpasdasdasd_postmeta ON ( wpasdasdasd_posts.ID = wpasdasdasd_postmeta.post_id )
WHERE 1=1
AND ( ( wpasdasdasd_postmeta.meta_key = 'wpcf-number' AND CAST(wpasdasdasd_postmeta.meta_value AS UNSIGNED) = '2' ) )
AND wpasdasdasd_posts.post_type = 'video'
AND (wpasdasdasd_posts.post_status = 'publish' OR wpasdasdasd_posts.post_status = 'private')
GROUP BY wpasdasdasd_posts.ID
ORDER BY wpasdasdasd_posts.post_date DESC LIMIT 0, 1

Note that the CAST(mt1.meta_value AS UNSIGNED) = '2' ) is not set to < '2' which seems to me like the logical thing. And WordPress doesn't display what I'm looking for: just one video that has a number smaller then 2.
Running WordPress 5.4 Is there something that I'm doing wrong here ? Thanks !

#1582229

My issue is resolved now. Thank you!