My posts have a custom field named production_date and formatted YYYYMMDD.
How can I form a meta_query to select only the YYYY part?
$meta_query_args = array( array( 'key' => 'production_date', 'value' => '2004', 'compare' => '???' ) );
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
The handy thing about how ACF stores dates as
YYYYMMDD is you can treat them like integers and get a similar level of functionality as if you were using "true" dates.
For example, to get all dates after 1st Jan 2011, use
> 20110101. Those before June 1st 2012?
< 20120601. And for your case, all dates within 2004?
>= 20040101 && <= 20041231.
Translated into a meta query:
$year = '2004'; $meta_query_args = array( array( 'key' => 'production_date', 'value' => array( $year . '0101', $year . '1231' ), 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ) );
Check out the codex for a full explanation of all the arguments.