1

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

paper123
  • 79
  • 6
  • Your code seems to be behaving like you would expect. The start date or end date does not fall between the 3rd and 10th. But you want it to show up if the filtered date range falls between the start and end date of a project right? You need some different comparing logic. Am I on the right track? – James Hamann Dec 10 '20 at 01:22
  • @JamesHamann yes, so its a case where none of the arguments fall inbetween, is there a way to still show the project in that case? with different logic – paper123 Dec 10 '20 at 07:31

0 Answers0