Enter your keyword

Blog

How to Order by Advanced Custom field in WP_QUERY

function my_pre_get_posts( $query ) {
	// do not modify queries in the admin
	if( is_admin() ) {	
		return $query;	
	}
	
	// only modify queries for 'event' post type
	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) {
		$query->set('orderby', 'meta_value');	
		$query->set('meta_key', 'p_start_date');	 
		$query->set('order', 'ASC'); 		
	}
		// return
	return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.