0

I am using custom post type with a relationship field with ACF and trying to get a column of number of times the "location" has been used in posts (this is my custom post type - location).

I have everything working beside the sortable option which I can't seem to figure out.

Any help will be appreciated, Here's my code so far:

function change_columns( $cols ) {
  $cols = array(
    'cb'       => '<input type="checkbox" />',
    'title'      => __( 'Title',      'trans' ),
    'number'      => __( 'Number',      'trans' ),
    'author'      => __( 'Author',      'trans' ),
    'Date'      => __( 'Date',      'trans' ),  
  );
  return $cols;
}
add_filter( "manage_edit-usa-locations_columns", "change_columns" );

function columns_content_only_locations($column_name, $post_ID) {
    if ($column_name == 'number') {

    $locations = get_posts(array(
        'post_type' => 'post',
        'category'  => '3',
        'numberposts'=>-1,
        'orderby' => 'title',
        'posts_per_page'   => -1,
        'meta_query' => array(
            array(
                'key' => 'headquarters', // name of custom field
                'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    ));

    $totalCount = count( $locations );
    echo $totalCount;

    }
}

add_action('manage_usa-locations_posts_custom_column', 'columns_content_only_locations', 10, 2);

// Make these columns sortable
function sortable_columns() {
  return array(
    'number'      => 'number',
    'title'      => 'title',
  );
}

add_filter( "manage_edit-usa-locations_sortable_columns", "sortable_columns" );


function number_column_order( $vars ) {
    if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'count',
            'orderby' => 'meta_value_num'
        ) );
    }

    return $vars;
}
add_filter( 'request', 'number_column_order' );

1 Answer 1

0

in the action request, the keys meta_key and orderby already exists then try to overwrite like this :

if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {

    $vars["meta_key"] = "count";
    $vars["orderby"] = "meta_value_num";

}
1
  • Tried it now, didn't work =/
    – Roy Savion
    Jul 9, 2016 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.