certainlyakey
1/24/2015 - 5:11 PM

Wordpress - Check if a custom query of selected post types is in action

Wordpress - Check if a custom query of selected post types is in action

if (custom_is_part_archive(array('seminar','event'))) {
	echo "this will appear in both seminar and event custom queries";
}
if (custom_is_part_archive('seminar')) {
	echo "this will appear only in seminar custom queries";
}

<section class="events">
<?php 
//Custom query...
$events_args = array('post_type' => 'event', 'numberposts' => 5 );
//use new WP_Query instead if you need to access the query variable later
$events = get_posts( $events_args );
//make custom query global
$custom_wp_query = $events;
p2p_type( 'posts_to_events' )->each_connected( $events, array(), 'news' );
if ($events) {
	echo '<h1>Events</h1>';
	foreach( $events as $post ) : setup_postdata($post); 
		Starkers_Utilities::get_template_parts( array( 'parts/post' ) );
	endforeach;
}
wp_reset_postdata();
?>
</section>
//Check if a custom query of selected post types is in action
function custom_is_part_archive($post_types = array()) {
	global $custom_wp_query;

	if ( isset($custom_wp_query) ) {
		if (!isset($post_types) || empty($post_types)) { //if no parameters at all
			return true;
		} else {
			if (!is_array($post_types)) { //if parameter is string
				$post_types_arr[] = $post_types;
			} else {
				$post_types_arr = $post_types;
			}
			global $post;
			foreach ($post_types_arr as $post_type) {
				if ($post->post_type == $post_type) {
					return true;
				}
			}
		}
	} else {
		return;
	}
}