Search code examples
phpmysqlwordpresswordpress-rest-api

How to write Wordpress Meta Queries with "if then" sorting logic


Summary

In my Wordpress build, I have a calendar made of custom event posts. These posts have two settings, single day event, and multiple day event. This is set as a boolean. I need to grab these posts, and sort them based on their date.

Problem

The issue I'm facing is that when I query the back-end, I don't know which date to sort by. I've tried using the boolean to create a sort of "if then" logic, but no luck.

Trouble Shooting

What have I tried so far?

This is the query I've been trying to get to work. No luck though

$events_query = new WP_Query (array(
    'post_type' => 'events',
    'posts_per_page' => '-1',
    'meta_query' => array (
        'relation' => 'AND',
        array (
            'key' => 'multiple_day_event',
            'value' => '1',
            'compare' => '=',
        ),
        array (
            'key' => 'start_date',
            'value' => date("Ymd"),
            'compare' => '>',
        ),
        'relation' => 'OR',
        array (
            'key' => 'multiple_day_event',
            'value' => '0',
            'compare' => '=',
        ),
        array (
            'key' => 'date',
            'value' => date("Ymd"),
            'compare' => '>',
        )
   )
));

Can I use a single date instead of two separate dates?

No, not to my knowledge. Using a single date in place of both has causing other issues in my back-end.

Solution

In short, I need a query that's able to use OR, AND, OR logic. Any ideas?


Solution

  • According to the WP_Meta_Query docs:

    Nested arrays can be used to construct complex queries

    $meta_query_args = array(
        'relation' => 'OR', // Optional, defaults to "AND"
        array(
            'key'     => '_my_custom_key',
            'value'   => 'Value I am looking for',
            'compare' => '='
        ),
        array(
            'relation' => 'AND',
            array(
                'key'     => '_my_custom_key_2',
                'value'   => 'Value I am looking for 2',
                'compare' => '='
            ),
            array(
                'key'     => '_my_custom_key_3',
                'value'   => 'Value I am looking for 3',
                'compare' => '='
            )
        )
    );
    $meta_query = new WP_Meta_Query( $meta_query_args );
    

    So your example would look something like this:

    $events_query = new WP_Query( array(
        'post_type' => 'events',
        'posts_per_page' => '-1',
        'meta_query' => array (
            'relation' => 'AND',
            array(
                'relation' => 'AND',
                array (
                    'key' => 'multiple_day_event',
                    'value' => '1',
                    'compare' => '=',
                ),
                array (
                    'key' => 'start_date',
                    'value' => date("Ymd"),
                    'compare' => '>',
                ),
            ),
            array(
                'relation' => 'OR',
                array (
                    'key' => 'multiple_day_event',
                    'value' => '0',
                    'compare' => '=',
                ),
                array (
                    'key' => 'date',
                    'value' => date("Ymd"),
                    'compare' => '>',
                )
            )
       )
    ) );