WP pre_get_posts Usage

, ,

Using pre_get_posts can be very useful for optimizing a query on a looped page.  It allows you to add limitations or custom post types to the main WP query allowing you to do what you have been doing in two steps into one single step.  Here is one example of how to add a custom post type to the blogs query loop.

// Make Testimonials show up on archive page
add_filter( 'pre_get_posts', 'wpcmsninja_add_custom_post_types_to_query' );
function wpcmsninja_add_custom_post_types_to_query( $query ) {
		is_archive() &&
		empty( $query->query_vars['suppress_filters'] )
	) {
		$query->set( 'post_type', array( 
		) );



Posted on

July 6, 2016

Submit a Comment

Your email address will not be published. Required fields are marked *