Home › Forums › ACF PRO › Getting number of times selected from relationship field › Reply To: Getting number of times selected from relationship field
After a few hours messing around with the code I got it working, the only thing I can’t seem to figure out is how to get it sortable, here’s what I got 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['order'] ) && 'number' == $vars['order'] ) {
$vars = array_merge( $vars, array(
'order' => 'ASC'
) );
}
return $vars;
}
add_filter( 'request', 'number_column_order' );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.