Home › Forums › General Issues › wp_query logic with acf fields
I’m currently building a pre_get_posts function that will do the following:
Filter out from the filters that I have set up but also I need some date filtering logic, for example if I have a project that is
from the 1st to the 15th as an example:
If
1. **Filter = from 2nd to 15th = Project shows up**
2. **Filter = from 1st to 12th = Project shows up**
3. **Filter = from 3rd to 10th = Project shows up**
4. **Filter = from 1st to the 15th = Project shows up**
Basically if the start date is between the project date or if the end date is between the project dates
This is the query that i have written so far:
` $meta_query = array(
‘relation’ => ‘AND’,
array(
array(
‘key’ => ‘place’,
‘value’ => $place,
‘compare’ => ‘LIKE’,
),
array(
‘key’ => ‘industry’,
‘value’ => $industry,
‘compare’ => ‘LIKE’,
),
array(
‘key’ => ‘type’,
‘value’ => $type,
‘compare’ => ‘LIKE’,
),
),
array(
‘relation’ => ‘OR’,
array(
‘key’ => ‘date_start’,
‘type’ => ‘DATE’,
‘value’ => $date_start_formatted,
‘compare’ => ‘=’,
),
array(
‘key’ => ‘date_end’,
‘type’ => ‘DATE’,
‘value’ => $date_end_formatted,
‘compare’ => ‘=’,
),
),
array(
‘relation’ => ‘OR’,
array(
‘key’ => ‘date_start’,
‘type’ => ‘DATE’,
‘value’ => array($date_start_formatted, $date_end_formatted),
‘compare’ => ‘BETWEEN’,
),
array(
‘key’ => ‘date_end’,
‘type’ => ‘DATE’,
‘value’ => array($date_end_formatted, $date_start_formatted),
‘compare’ => ‘BETWEEN’,
),
),`
but the logic seems to fail if i select the option 3 from the examples, the project does not show up.
Would greatly appreciate some help or directions
Is it a date field or a date time field?
Date fields in ACF are not stored in standard DB date formats. You cannot use 'type' => 'DATE'
with these fields. You need to use wither number or char as type. The format of the value in the DB is “YYYYMMDD” or in standard date notation ‘Ymd’
You must be logged in to reply to this topic.
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.