1

I'm creating a Custom Post for an event calendar. My Custom Post has 2 custom meta fields called "event_star_date" & "event_end_date". I want to use pre_get_posts to exclude Events which their "event_end_date" is lower than todays date OR "event_end_date" is empty and "event_start_date" is lower than todays date. I have see some codes but the don't finaly work for me.

Thanks to @cybnet I have nearle get the code I need, but steel not working. What I would nedd could be something like this:

function bbwp_calendar_visible_events( $query ) {
if ( !is_admin() && $query->is_main_query() ){
    if ( is_post_type_archive( $postType ) ) {
            $meta_query = array(
            'relation' => 'OR',
            array(
                'relation' => 'AND',
                array(
                    'key'   => 'event_start_date',
                    'value' => current_time( 'timestamp' ),
                    'type' => 'CHAR',
                    'compare' => '>=',
                ),
                array(
                    'key'   => 'event_end_date',
                    'value' => '',
                    'type' => 'CHAR',
                    'compare' => '=',
                ),
            ),
            array(
                'key'   => 'event_end_date',
                'value' => current_time( 'timestamp' ),
                'type' => 'CHAR',
                'compare' => '>=',
            ),
        );

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

add_action( 'pre_get_posts', 'bbwp_calendar_visible_events' );

Thanks to every body

  • 1
    What about just fetching all items and checking the date before the output of the posts? – Hannes Apr 25 '14 at 9:50
  • @Hannes that is not good solution. Imaging the performance issues if you have a lot of events. You will run a big query to get all the events and then you will run a loop over all of them to filter only the events you want in the output. Make the correct query is the best option. – cybmeta Apr 25 '14 at 9:57
  • You can use post__not_in with pre_get_posts – Brad Dalton Apr 25 '14 at 14:32
1

Your can use the "meta_query" argument of the query, I think is better than filter the post_where:

add_action( 'pre_get_posts', 'bbwp_calendar_visible_events' );
function bbwp_calendar_visible_events( $query ) {

    $postType = 'bbwp_calendar';

    if ( !is_admin() && $query->is_main_query() ){

        if ( is_post_type_archive( $postType ) ) {

            $meta_query = array(
                              'relation' => 'OR',
                              //current time <= event_end_date
                              array(
                                  'key'   => 'event_end_date',
                                   //Check https://codex.wordpress.org/Function_Reference/current_time
                                   //to return the current time (today) in the same format
                                   //you store the date in your custom field
                                  'value' => current_time( 'timestamp' ),
                                  'type' => 'DATETIME',
                                  'compare' => '<=',
                              ),
                              //event_end_date = ''
                              array(
                                  'key'   => 'event_end_date',
                                   //Check https://codex.wordpress.org/Function_Reference/current_time
                                   //to return the current time (today) in the same format
                                   //you store the date in your custom field
                                  'value' => "",
                                  'type' => 'DATETIME',
                                  'compare' => '=',
                              ),
                     );

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

         }
     }
} 

More info:

  • Thank you bery much. I have nearly got my code... but steal not working XD – Nasgor Apr 25 '14 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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