• Hello,
    I’ve created categories (eg. “Rando”) and also custom fields with ACf (eg. “kilometrage”).
    Now i try to sort my articles “km” by “kilometrage” and i put the code below in Code Snippets extension. I also try in my fonction.php file of my theme (Bateaux theme).
    But that doesn’t work.
    Could you please help me ?
    Thanks a lot

    function my_pre_get_posts($query) {
    	if(is_admin()) {
    		return $query;
    	}
    	if(isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'km' && $query->is_main_query()) {
    		$query->set('orderby', 'meta_value');
    		$query->set('meta_key', 'kilometrage');
    		$query->set('order', 'ASC');
    	}
    	return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_posts');
Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Unfortunately, without being able to know more about how your site is configured and how these post types are set up, this is a little difficult to answer.

    The fact that it doesn’t work in functions.php or with this plugin indicates that there’s a mismatch between the underlying ACF configuration and this code. I’d recommend ensuring that the post type and meta field names match exactly as the first thing to try.

Viewing 1 replies (of 1 total)
  • The topic ‘Sort by … ACF custom Field’ is closed to new replies.