I have a query using 'compare' -- trying to select where meta values are between certain dates:
$argsTwo = array(
'post_type' => 'event',
'meta_key' => '_event_information_year_select',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => '_event_information_year_select',
'value' => 1999,
'compare' => '>'
),
array(
'key' => '_event_information_year_select',
'value' => 2003,
'compare' => '<='
)
)
)
);
My issue is this query only gives me results until 2002. I can't seem to figure out what is going wrong. The values definitely exist in the DB but these $args don't seem to work.
Any ideas?
Thanks
I have a query using 'compare' -- trying to select where meta values are between certain dates:
$argsTwo = array(
'post_type' => 'event',
'meta_key' => '_event_information_year_select',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => '_event_information_year_select',
'value' => 1999,
'compare' => '>'
),
array(
'key' => '_event_information_year_select',
'value' => 2003,
'compare' => '<='
)
)
)
);
My issue is this query only gives me results until 2002. I can't seem to figure out what is going wrong. The values definitely exist in the DB but these $args don't seem to work.
Any ideas?
Thanks
Share Improve this question asked Jul 14, 2019 at 18:15 Best Dev TutorialsBest Dev Tutorials 4451 gold badge7 silver badges21 bronze badges 2 |1 Answer
Reset to default 1It looks like your meta_query is nested within an extra array. Aside from that, the code looks valid.
Hope this helps:
$argsTwo = array(
'post_type' => 'event',
'meta_key' => '_event_information_year_select',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_event_information_year_select',
'value' => 1999,
'compare' => '>'
),
array(
'key' => '_event_information_year_select',
'value' => 2003,
'compare' => '<='
)
)
);
event_information_year
taxonomy with terms for each year, then you could make atax_query
that's potentially 100x faster, easier to write, and provides a super easy to way to list the years in a UI on the frontend, as well as providing you with dedicated archive templates, and REST API endpoints? – Tom J Nowell ♦ Commented Jul 14, 2019 at 18:56