When your website means business.

WP Query | Type: PHPPre Get Posts

Set up query parameters for different CPT archives.

/**
* @param $query
 */
function sfcc_pre_get_posts($query) {
	if(!is_admin() ) {

	// Directory Archive
	if($query->is_main_query() && is_post_type_archive('person')) {

		$query->set('orderby', 'meta_value');
		$query->set('meta_key', 'last_name');
		$query->set('order','ASC');
		$query->set('posts_per_page', -1);
		$query->set('nopaging', TRUE);
	}

	// Degrees Archive
	if($query->is_main_query() && is_post_type_archive('degree')) {

		$query->set('orderby','title');
		$query->set('order','ASC');
		$query->set('posts_per_page', 100);
	}

	// Press Page
	if( $query->is_main_query() && is_post_type_archive( 'press' )) {
		$query->set( 'orderby', 'date' );
		$query->set( 'order', 'DESC' );
		$query->set('posts_per_page', 20);
	}

	// Policies Archive
	if ($query->is_main_query() && is_post_type_archive('policy')) {
            $query->set('meta_key', 'policy_number' );
            $query->set('orderby', 'meta_value');
            $query->set('order', 'ASC');
            $query->set('posts_per_page', -1);
	    $query->set('nopaging', TRUE);
	}
		
	// Policies Taxonomy
	if ($query->is_post_type_archive('policy')) {
            $query->set('tax_query', 'policy_section');
            $query->set('meta_key', 'policy_number');
            $query->set('orderby', 'meta_value');
            $query->set('order', 'ASC');
            $query->set('posts_per_page', -1);
	    $query->set('nopaging', TRUE);
	}
    }
}
add_action('pre_get_posts', 'sfcc_pre_get_posts');
Menu
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Cookies Notice