Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Sort by … ACF custom Field

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 59525

Trending Articles