Home › Forums › General Issues › Filter posts by just month and year values from Datepicker Custom field › Reply To: Filter posts by just month and year values from Datepicker Custom field
I achieved this in a meta_query by using REGEXP. I couldn’t find a way to do it with just dates and a range, because like you I wanted it to be year-agnostic.
The date format by default is stored as YYYYMMDD. The following searches for a string like $$$$MM$$, where MM is your month. In other words, any 4 digits, followed by a specified 2-digit month, followed by any 2 digits.
$filter_month = '09'; // show september only
$args = array (
'post_type' => 'course', // your custom post type
'meta_key' => 'course_date', // your custom date field name
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'course_date',
'compare' => 'REGEXP',
'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}',
),
)
);
$posts = get_posts($args);
A little late, but hope this helps.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.