Support

Account

Home Forums General Issues wp_query logic with acf fields

Helping

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’

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.