Support

Account

Home Forums General Issues Order by ACF field

Helping

Order by ACF field

  • Hi,

    I’d like to order my loop (for a CPT, produit) using an ACF Field (a number, irank).

    I am using this function but so far, this is not working :

    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'] == 'produit' ) {
    		
    		$query->set('orderby', 'meta_value_num');	
    		$query->set('meta_key', 'irank');	 
    		$query->set('order', 'ASC'); 
    		
    	}
    	
    
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');

    Any idea on what is the issue ?

    Thanks

  • Your code looks correct to do what you describe. In what way doesn’t it work?

    What template is running the loop?
    Are you sure the filter code is running? You could try placing a var_dump inside the filter inside the second if check and see if it shows on the page.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Order by ACF field’ is closed to new replies.