Display search results for only upcoming events

Home Forums Calendar Products Events Calendar PRO Display search results for only upcoming events

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #12998
    Kyle
    Member

    I want to display events in my general WordPress search results. I have that working with no problem. I just can’t seem to figure out how to only return events with today’s or a future event date. Can anyone assist me with that? Thank you for your help!

    #13053
    Rob
    Member

    Hey Kyle. Thanks for the note; interesting request. I’m not aware of how this could be done off-hand but let me see if our developer Jonah has any suggestions. I’ll get him to chime in directly.

    #13100
    Jonah
    Participant

    Hi Kyle,

    You’ll need to hook into the pre_get_posts action like so: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    You’ll want to conditionally apply the query changes only for is_search() and pass in additional parameters to specify for example only upcoming events (‘eventDisplay’=>’upcoming’).

    If you also want to apply query filtering by date you can use ‘start_date’=>’1/9/2012’ & ‘end_date’=>’1/9/2012’ for example (you’ll want to change the dates and dynamically populate with todays date).

    #13134
    Kyle
    Member

    Jonah, thank you for the response. I added the following with no luck. Did I misunderstand what you posted? Thank you for your help!

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $query->set( ‘eventDisplay’, ‘upcoming’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    Nothing happens with this one.

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $query->set(‘eventDisplay’=>’upcoming’);
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    I get this error: “Parse error: syntax error, unexpected T_DOUBLE_ARROW”

    #13153
    Kyle
    Member

    I also tried to do it by date with no luck –

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $yesterday_before_midnight = date(“Y-m-d”, mktime(0, 0, 0, date(“m”),date(“d”)-1,date(“Y”))).’ 23:59:59′;
    $meta = array(
    array(
    ‘key’ => ‘_EventStartDate’,
    ‘value’ => $yesterday_before_midnight,
    ‘compare’ => ‘>’
    )
    );
    $query->set( ‘meta_query’, $meta );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    #13182
    Kyle
    Member

    Ok, I got it to work!!! This is the code to make it happen –

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $yesterday_before_midnight = date(‘Y-m-d’, mktime(0, 0, 0, date(‘m’),date(‘d’)-1,date(‘Y’))).’ 23:59:59′;
    $meta = array(
    array(
    ‘key’ => ‘_EventStartDate’,
    ‘value’ => $yesterday_before_midnight,
    ‘compare’ => ‘>’
    )
    );
    $query->set( ‘meta_query’, $meta );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    #13184
    Kyle
    Member

    Ok one small problem, this code will only posts/pages with the meta_key “_EventStartDate”. How do I get it to only apply posts that are events?

    #13185
    Kyle
    Member

    Sorry let me be more clear, since I posted my yahoo to soon. If you use this code, only posts that are events or have the meta_key “_EventStartDate” are returned in the results. How do I get this to only apply to event posts instead of all posts?

    #13190
    Jonah
    Participant

    Hey Kyle,

    Include this in your query set:


    $query->set( 'post_type','tribe_events' );

    #13191
    Kyle
    Member

    I have done this and it doesn’t work it just redirects me to the default events page with the upcoming events listed. Sorry for the hassle. Here is what I have –

    function exclude_old_events( $query ) {
    if ( $query->is_search && (‘tribe_events’ == get_post_type()) ) {
    $yesterday_before_midnight = date(‘Y-m-d’, mktime(0, 0, 0, date(‘m’),date(‘d’)-1,date(‘Y’))).’ 23:59:59′;
    $meta = array(
    array(
    ‘key’ => ‘_EventStartDate’,
    ‘value’ => $yesterday_before_midnight,
    ‘compare’ => ‘>’
    )
    );
    $query->set( ‘meta_query’, $meta );
    $query->set( ‘post_type’,’tribe_events’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    #13192
    Kyle
    Member

    Sorry that is test code. Here is what I have –

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $yesterday_before_midnight = date(‘Y-m-d’, mktime(0, 0, 0, date(‘m’),date(‘d’)-1,date(‘Y’))).’ 23:59:59′;
    $meta = array(
    array(
    ‘key’ => ‘_EventStartDate’,
    ‘value’ => $yesterday_before_midnight,
    ‘compare’ => ‘>’
    )
    );
    $query->set( ‘meta_query’, $meta );
    $query->set( ‘post_type’,’tribe_events’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );

    #13193
    Jonah
    Participant

    Try combining the parameters like this:


    $query->set( array( ‘meta_query’=>$meta, 'post_type'=>'tribe_events' ) );

    #13195
    Jonah
    Participant

    And make sure those single quotes are proper single quotes…

    #13196
    Kyle
    Member

    Sorry, that doesn’t work either. It throws the error “Warning: Missing argument 2 for WP_Query::set()”

    #13197
    Kyle
    Member

    Sorry, that doesn’t work either. It throws the error “Warning: Missing argument 2 for WP_Query::set()”

    function exclude_old_events( $query ) {
    if ( $query->is_search ) {
    $yesterday_before_midnight = date(‘Y-m-d’, mktime(0, 0, 0, date(‘m’),date(‘d’)-1,date(‘Y’))).’ 23:59:59′;
    $meta = array(
    array(
    ‘key’ => ‘_EventStartDate’,
    ‘value’ => $yesterday_before_midnight,
    ‘compare’ => ‘>’
    )
    );
    $query->set( array( ‘meta_query’=>$meta, ‘post_type’=>’tribe_events’ ) );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_old_events’ );
    ?>

Viewing 15 posts - 1 through 15 (of 23 total)
  • The topic ‘Display search results for only upcoming events’ is closed to new replies.