In case it saves anyone else the time, adding the following filter on pre_get_posts
as an admin only code snippet got this working for me.
add_action( 'pre_get_posts', 'my_sort_admin_column' );
function my_sort_admin_column( $query ) {
/**
* We only want our code to run in the main WP query
* AND if an orderby query variable is designated.
*/
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
// If we're ordering by 'shared_counts_total'
case 'shared_counts':
// Setting just <code>meta_key</code> is not sufficient, as this
// will ignore posts that do not yet, or never will have
// a value for the specified key. This meta query will
// register the <code>meta_key</code> for ordering, but will not
// ignore those posts without a value for this key.
$query->set( 'meta_query', array(
'relation' => 'OR',
array(
'key' => 'shared_counts_total',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'shared_counts_total',
'compare' => 'EXISTS'
),
) );
// Order by the meta value, then by the date if multiple
// posts share the same value for the provided meta key.
// Use <code>meta_value_num</code> since the meta value
// for shared_counts are numeric.
$query->set( 'orderby', 'meta_value_num date' );
break;
}
}
}
The nice function using ‘switch’ comes from wpdreamer and was helpful since I had columns from another plugin with the same issue. This answer on stackexchange provided the solution for getting the posts without a shared_count_total to show.
-
This reply was modified 6 years ago by sloweye. Reason: corrections to comments in code
-
This reply was modified 6 years ago by sloweye.
-
This reply was modified 6 years ago by sloweye.