0
votes

I'm using visual composer by http://vc.wpbakery.com/ on my wordpress site. My issue is within the custom query of the post grid.

Basically I'm selecting all upcoming events (post type event) where the event-end-date (custom field) is >= today.

My query string (unescaped):

paged=1&posts_per_page=-1&offset=0&post_status=publish&ignore_sticky_posts=0&orderby=meta_value&order=DESC&post_type[event]=event&meta_key=event-end-date&meta_query[0][key]=event-end-date&meta_query[0][value]=2016-09-20&meta_query[0][compare]=>=&meta_query[0][type]=DATE

The thing that bums me out is that the today date is set fixed to: 2016-09-20. Now everytime an event passes I manually have to update this date so that the event isn't represented in the upcoming list any more.

Is there a possibility to set a changing value like today's date in a wordpress query string?

VC only allows me to insert a query string so I can't work with variables, except there is some sort of mechanism which allows me replace some template-keyword with the current date.

Or is there some other way how I can filter the upcoming events, like using tags, post meta values, etc...?

-- Solved --

  • update events every 12 hours
  • update events if saved

functions.php

add_filter('cron_schedules', 'my_cron_schedules');
function my_cron_schedules( $schedules ) {
    $schedules['halfday'] = array(
        'interval' => 43200,
        'display' => __('halfday')
    );
    return $schedules;
}

if ( !wp_next_scheduled( 'my_halfday_event' ) ) {
    wp_schedule_event( time(), 'halfday', 'my_halfday_event' );
}
add_action('my_halfday_event', 'update_events');

function save_event( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( $post_type != 'event' ) {
        return;
    }
    update_events();
}
add_action( 'save_post', 'save_event' );

function update_events() {
    $events = get_posts( array(
        'posts_per_page' => -1,
        'post_type' => 'event'
    ) );
    foreach( $events as $event ) {
        $event_end_date = get_post_field( 'event-end-date', $event->ID );
        if ( is_wp_error( $event_end_date ) ) {
            continue;
        }
        $today = current_time('Y-m-d');
        $event_expired = $event_end_date < $today ? 'true' : 'false';
        $event_updated = current_time('Y-m-d H:i:s');
        update_post_meta($event->ID, 'event-expired', $event_expired);
        update_post_meta($event->ID, 'event-updated', $event_updated);
    }
}

new query

paged=1&posts_per_page=-1&offset=0&post_status=publish&ignore_sticky_posts=0&orderby=meta_value&order=DESC&post_type%5Bevent%5D=event&meta_key=event-end-date&meta_query%5B0%5D%5Bkey%5D=event-expired&meta_query%5B0%5D%5Bvalue%5D=false&meta_query%5B0%5D%5Bcompare%5D=%3D&meta_query%5B0%5D%5Btype%5D=CHAR
1

1 Answers

0
votes

I ran across the same issue a while back and used DATE=$today in my custom query and it works fine. Take a look at the problem and solution posted here: Visual Composer custom query with OR operator