1

I have the following query_args to fetch events: -> either it's a single event (only start date), so it checks if the start date is between the start of the month AND the end of the month -> or it's a duration event (start + end date), so it checks if the start date is smaller than the start of the month AND the end date is bigger than the start of the month (-> outputs if a event is currently ongoing)

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'event',
    'orderby' => 'meta_value',
    'meta_query' => array(
        'relation' => 'OR',
        // single event
        array(
            'relation' => 'AND',
            array(
                'key' => 'date_start',
                'value' => $firstDayThisMonth,
                'compare' => '>='
            ),
            array(
                'key' => 'date_start',
                'value' => $lastDayThisMonth,
                'compare' => '<='
            )
        ),
        // duration event
        array(
            'relation' => 'AND',
            array(
                'key' => 'date_start',
                'value' => $firstDayThisMonth,
                'compare' => '<='
            ),
            array(
                'key' => 'date_end',
                'value' => $firstDayThisMonth,
                'compare' => '>='
            )
        )
    ),
    'order' => 'ASC'
);

This worked flawlessly on my dev-Server, but on production it doesnt output any events anymore. I have to remove the //duration event array to output the single events, but both queries wont work together. Any ideas?

2
  • Do you have the same events in production? What are their dates? How are $firstDayThisMonth and $lastDayThisMonth defined? Commented May 12, 2021 at 8:52
  • A difference between environments is usually caused by different server settings. Check the timezones on both of your environments. Also check if php versions and loaded modules/extensions are the same. It might be as simple as a missing function that you're not seeing because of the error suppression on the live server (so, check the webserver's logs also)
    – Jos
    Commented May 12, 2021 at 14:33

1 Answer 1

0

My reputation is too low for me to comment so I'll do it this way.

Make sure that the date format inside 'value' is the same as the date formats inside the database fields. Also don't forget to add the 'type' => 'DATE' to your query. An example of some code that I've used in the past:

$queryArgs['meta_query'] = [
    [
        'relation' => 'OR',
        [
            'key' => '_ys_brochures-offline-date',
            'value' => date('Y-m-d'),
            'compare' => '>',
            'type' => 'DATE' // notice the 'DATE'
        ],
        [
            'key' => '_ys_brochures-offline-date',
            'compare' => 'NOT EXISTS',
        ],
    ]
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.