Modifying queries with pre_get_posts

Written by Remi Corson.

you can for example query only posts from a specific type and with a custom meta field key equal to a determined value. In the code below, we are targeting only custom post type called “portfolio” where meta key “project_type” is set to “design”, that’s a typical query that can be used on a portfolio page.

// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $query ) {

	// Check if on frontend and main query is modified
	if( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] != 'portfolio' ) {

		$query->set('meta_key', 'project_type');
		$query->set('meta_value', 'design');




Tags: ,

No comments yet.

Leave a Reply