Skip to content

Instantly share code, notes, and snippets.

@nerrad
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerrad/9ec4e10cc4ba16650484 to your computer and use it in GitHub Desktop.
Save nerrad/9ec4e10cc4ba16650484 to your computer and use it in GitHub Desktop.
Filter for injecting Event Espresso event post type events into everywhere posts are queried in WP
<?php
/**
* This is an alternative filter for adding events to posts.
*
* 1. Adds events to ANY query for posts (including core wp functions like wp_recent_posts() that suppress filters ).
* 2. Ensures we don't accidentally add posts (and events) into custom queries for other post types. (eg sliders).
*/
function add_espresso_events_to_posts( $WP_Query ) {
if ( $WP_Query instanceof WP_Query && ( $WP_Query->is_feed || $WP_Query->is_posts_page || ( $WP_Query->is_home && ! $WP_Query->is_page ) || ( isset( $WP_Query->query_vars['post_type'] ) && ( $WP_Query->query_vars['post_type'] == 'post' || is_array( $WP_Query->query_vars['post_type'] ) && in_array( 'post', $WP_Query->query_vars['post_type'] ) ) ) ) ) {
//if post_types ARE present and 'post' is not in that array, then get out!
if ( isset( $WP_Query->query_vars['post_type'] ) && $post_types = (array) $WP_Query->query_vars['post_type'] ) {
if ( ! in_array( 'post', $post_types ) ) {
return;
}
} else {
$post_types = array( 'post' );
}
if ( ! in_array( 'espresso_events', $post_types )) {
$post_types[] = 'espresso_events';
$WP_Query->set( 'post_type', $post_types );
}
return;
}
}
add_action( 'pre_get_posts', 'add_espresso_events_to_posts', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment