I'm trying to sort my columns alphabetically, but when I use the pre_get_posts()
function it breaks the site. I imagine this is because the ACF I'm using is just a 2 letter text field and I don't know how to reference that in the function
function get_geoCode($post_ID) {
$geoCode = the_field('geo_code');
if ($geoCode) {
return $geoCode;
}
}
function geo_row($defaults) {
$defaults['geo_col'] = 'Geo Code';
return $defaults;
}
function geo_row_content($column_name, $post_ID) {
if ($column_name == 'geo_col') {
$geo_code_id = get_geoCode($post_ID);
if ($geo_code_id) {
echo $geo_code_id;
}
}
}
// CALL THE ABOVE FUNCTIONS + FILTERS
add_filter('manage_page_posts_columns', 'geo_row', 10);
add_filter('manage_edit-page_sortable_columns', 'geo_columns');
function geo_columns( $columns ) {
$columns['geo_col'] = 'Geo Code';
//To make a column 'un-sortable' remove it from the array
//unset($columns['date']);
return $columns;
}
add_action('manage_page_posts_custom_column', 'geo_row_content', 10, 2);
add_action( 'pre_get_posts', 'geoCol_orderby' );
function geoCol_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'Geo Code' == $orderby ) {
$query->set('meta_key','geo_col');
$query->set('orderby','meta_value_num');
}
}
So the column shows with the ACF text value but it sorts by something that isn't alphabetical. If I enable the pre_get_posts function it breaks the site.
Any idea how to get an alphabetical value from an ACF text field?