Display CPT in admin, ordered by a numeric ACF field

[ad_1]

I have a CPT ‘eagle-scout’. It has a custom field (created with ACF) called ‘eagle\_scout\_number-‘. In the WordPress admin screen for this CPT, I want to display all posts sorted by this custom field in descending order. I am using the following code, which works, mostly. However, it sorts “9” above 80-89, and “8” above 70-19, etc. Is there a way to modify this code so that it will sort the single-digit number numerically with the double-digit numbers? I don’t want to add preceding zeros as we are approaching 100, and adding two zeroes in front of 1-9 would make them look like secret agents.

​

// Force order for Eagle Scout custom post types on Admin
function s334_order_post_type($query) {
if($query->is_admin) {

if ($query->get(‘post_type’) == ‘eagle-scout’)
{
$query->set( ‘meta_key’, ‘eagle_scout_number-‘);
$query->set(‘orderby’, ‘meta_value’);
$query->set(‘order’, ‘DESC’);
}
}
return $query;
}
add_filter(‘pre_get_posts’, ‘s334_order_post_type’);

​

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer