Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active March 18, 2025 20:38
Show Gist options
  • Save markhowellsmead/2bb4abdc8b718ee3e20c7cc4cf58be6b to your computer and use it in GitHub Desktop.
Save markhowellsmead/2bb4abdc8b718ee3e20c7cc4cf58be6b to your computer and use it in GitHub Desktop.

Revisions

  1. markhowellsmead revised this gist Oct 22, 2019. 1 changed file with 11 additions and 24 deletions.
    35 changes: 11 additions & 24 deletions event_query.php
    Original file line number Diff line number Diff line change
    @@ -56,38 +56,25 @@ public function onlyFutureEntries($query)
    // Start date >= today and end date empty
    'relation' => 'AND',
    [
    'relation' => 'OR',
    [
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '>=',
    'type' => 'DATE'
    ]
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '>=',
    'type' => 'DATE'
    ],
    [
    'relation' => 'OR',
    [
    'key' => 'end_date',
    'value' => '',
    'compare' => '='
    ],
    [
    'key' => 'end_date',
    'compare' => 'NOT EXISTS',
    ]
    'key' => 'end_date',
    'value' => '',
    'compare' => '='
    ]
    ],
    [
    // Start date <= today and end date >= today
    'relation' => 'AND',
    [
    'relation' => 'OR',
    [
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '<=',
    'type' => 'DATE'
    ]
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '<=',
    'type' => 'DATE'
    ],
    [
    'key' => 'end_date',
  2. markhowellsmead revised this gist Oct 22, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions event_query.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,10 @@
    * [email protected] 22.10.2019, based on code from 201 onwards
    */

    add_action('pre_get_posts', [$this, 'onlyFutureEntries']);


    public function onlyFutureEntries($query)
    {
    if (!is_admin() && isset($query->query['post_type']) && $query->query['post_type'] === 'sht_event') {
  3. markhowellsmead created this gist Oct 22, 2019.
    99 changes: 99 additions & 0 deletions event_query.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    <?php

    /**
    * Complex WordPress meta query by start and end date (custom meta fields)
    * Intended for use on the `pre_get_posts` hook.
    * Caution; this makes the query very slow - several seconds - so should be
    * implemented with some form of caching.
    *
    * [email protected] 22.10.2019, based on code from 201 onwards
    */

    public function onlyFutureEntries($query)
    {
    if (!is_admin() && isset($query->query['post_type']) && $query->query['post_type'] === 'sht_event') {
    $query->set('orderby', 'meta_value');
    $query->set('order', 'ASC');
    $query->set('meta_key', 'start_date');

    $meta_query = (array) $query->get('meta_query');

    $meta_query[] = [
    'relation' => 'OR',
    [
    // Start date and end date are empty
    'relation' => 'AND',
    [
    'relation' => 'OR',
    [
    'key' => 'start_date',
    'value' => '',
    'compare' => '='
    ],
    [
    'key' => 'start_date',
    'compare' => 'NOT EXISTS'
    ]
    ],
    [
    'relation' => 'OR',
    [
    'key' => 'end_date',
    'value' => '',
    'compare' => '='
    ],
    [
    'key' => 'end_date',
    'compare' => 'NOT EXISTS',
    ]
    ]
    ],
    [
    // Start date >= today and end date empty
    'relation' => 'AND',
    [
    'relation' => 'OR',
    [
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '>=',
    'type' => 'DATE'
    ]
    ],
    [
    'relation' => 'OR',
    [
    'key' => 'end_date',
    'value' => '',
    'compare' => '='
    ],
    [
    'key' => 'end_date',
    'compare' => 'NOT EXISTS',
    ]
    ]
    ],
    [
    // Start date <= today and end date >= today
    'relation' => 'AND',
    [
    'relation' => 'OR',
    [
    'key' => 'start_date',
    'value' => date('Y-m-d'),
    'compare' => '<=',
    'type' => 'DATE'
    ]
    ],
    [
    'key' => 'end_date',
    'value' => date('Y-m-d'),
    'compare' => '>=',
    'type' => 'DATE'
    ]
    ]
    ];

    $query->set('meta_query', $meta_query);
    }
    }