Home › Forums › Calendar Products › Events Calendar PRO › Display search results for only upcoming events
- This topic has 22 replies, 5 voices, and was last updated 10 years, 9 months ago by
Support Droid.
-
AuthorPosts
-
January 7, 2012 at 8:35 pm #12998
Kyle
MemberI 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!
January 9, 2012 at 4:21 pm #13053Rob
MemberHey 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.
January 9, 2012 at 7:18 pm #13100Jonah
ParticipantHi 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).
January 10, 2012 at 12:27 pm #13134Kyle
MemberJonah, 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”
January 10, 2012 at 2:09 pm #13153Kyle
MemberI 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’ );January 11, 2012 at 8:02 am #13182Kyle
MemberOk, 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’ );January 11, 2012 at 8:16 am #13184Kyle
MemberOk 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?
January 11, 2012 at 8:18 am #13185Kyle
MemberSorry 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?
January 11, 2012 at 10:24 am #13190Jonah
ParticipantHey Kyle,
Include this in your query set:
$query->set( 'post_type','tribe_events' );
January 11, 2012 at 10:29 am #13191Kyle
MemberI 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’ );January 11, 2012 at 10:30 am #13192Kyle
MemberSorry 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’ );January 11, 2012 at 10:34 am #13193Jonah
ParticipantTry combining the parameters like this:
$query->set( array( ‘meta_query’=>$meta, 'post_type'=>'tribe_events' ) );
January 11, 2012 at 10:34 am #13195Jonah
ParticipantAnd make sure those single quotes are proper single quotes…
January 11, 2012 at 10:36 am #13196Kyle
MemberSorry, that doesn’t work either. It throws the error “Warning: Missing argument 2 for WP_Query::set()”
January 11, 2012 at 10:40 am #13197Kyle
MemberSorry, 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’ );
?> -
AuthorPosts
- The topic ‘Display search results for only upcoming events’ is closed to new replies.
