简体   繁体   中英

Sortable Admin Row From ACF Wordpress - (Alphabetical Sort)

I have a custom column added to my Wordpress admin page. It is a text field with just text.

When I try to sort it alphabetically it only sorts by date created.

function get_geoCode($post_ID) {
    $geoCode = get_field('geo_code', $post_ID);
    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 = get_geoCode($post_ID);
        if ($geo_code) {
            echo  $geo_code;
        }
    }
}


  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');
    }
}
    add_action( 'pre_get_posts', 'geoCol_orderby' );




// CALL THE ABOVE FUNCTIONS +  FILTERS
add_filter('manage_page_posts_columns', 'campaign_id_row', 10);
add_action('manage_page_posts_custom_column', 'campaign_row_content', 10, 
2);

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';
    return $columns;
}

add_action('manage_page_posts_custom_column', 'geo_row_content', 10, 2);

This creates the Geo-code Col but If I run geoCol_orderby() it breaks the site. Presumably because there is no data for the func to access?

I've tried orderby meta_value and meta_value_num and alphabetical. All break the site.

It turns out My metaKey

$query->set('meta_key','geo_col');

Was wrong and shouldve been geo_code as stated on the ACF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM