Wordpress: get one record that meets certain rules

Viewed 30

Friends, I ask for help in an unusual task.

Wordpress and a custom post type events. Events have several ACF fields: e_date (event date), e_active (true/false field - real event) and e_forecast (true/false field - event forecast). When filling in an event, e_date (a required field) is filled in and e_active or e_forecast is set, depending on the type of event. So, I need to get all the event records, check these records for compliance with certain rules and output one last record. The rules are as follows:

  1. Check all entries with e_active by date. I.e. compare e_date with the current date. If there is an e_active event and with an e_date that has not yet occurred, then we output it.

  2. If all e_active with e_date have passed, i.e. they do not correspond to point 1, then we check e_forecast and e_date according to the same principle.

  3. If 1 and 2 don't fit, then just else {}

At the moment, I get 1 last published record, but often e_forecast is published later than e_active, although e_active has not yet reached e_date.

$events = get_posts([
    'post_type'         => 'events',
    'numberposts'       => 1,
    'meta_query'        => [
        'relation' => 'OR',
        [
            'key'       => 'e_active',
            'value'     => true
        ],
        [
            'key'       => 'e_forecast',
            'value'     => true
        ]
    ]
]);

Unfortunately, I myself can't even imagine how to sort through, say, the last 10 records for compliance with the described rules. I really hope your help)

0 Answers
Related