Title: posts hidden when sorting by shared counts column
Last modified: October 4, 2018

---

# posts hidden when sorting by shared counts column

 *  Resolved [sloweye](https://wordpress.org/support/users/sloweye/)
 * (@sloweye)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/)
 * This plugin is a great solution for working with shared counts in the admin. 
   It’s quite useful to be able to sort posts by shared counts on the all posts 
   screen in the admin.
 * However, any posts for which no shared count is returned are being hidden when
   the shared count column is sorted.
 * Is it possible to have the plugin return “—” the way WordPress does for the comment
   count when it is empty?
 * I’ve tried using the the filter for shared_counts_total, but not having any luck.
 * Thanks

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [sloweye](https://wordpress.org/support/users/sloweye/)
 * (@sloweye)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-10927747)
 * In case it saves anyone else the time, adding the following filter on `pre_get_posts`
   as an admin only [code snippet](https://wordpress.org/plugins/code-snippets/)
   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](https://wpdreamer.com/2014/04/how-to-make-your-wordpress-admin-columns-sortable/)
   and was helpful since I had columns from another plugin with the same issue. 
   This [answer on stackexchange](https://wordpress.stackexchange.com/a/301163/144711)
   provided the solution for getting the posts without a shared_count_total to show.
    -  This reply was modified 7 years, 6 months ago by [sloweye](https://wordpress.org/support/users/sloweye/).
      Reason: corrections to comments in code
    -  This reply was modified 7 years, 6 months ago by [sloweye](https://wordpress.org/support/users/sloweye/).
    -  This reply was modified 7 years, 6 months ago by [sloweye](https://wordpress.org/support/users/sloweye/).
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-11463812)
 * Thanks for the message. I just tried implementing this in Shared Counts but it
   didn’t work as expected. The zero count posts did remain, but they appear in 
   the wrong order.
 * When sorting in DESC order (largest at top) it goes zero count posts, high count
   posts, low count posts: [https://cl.ly/688079b025df](https://cl.ly/688079b025df)
 * When sorting in ASC order (smallest at top) it goes low count posts, high count
   posts, zero count posts: [https://cl.ly/d9c279abd826](https://cl.ly/d9c279abd826)
 * To implement this change, I modified [the sort_column_query() method](https://github.com/jaredatch/Shared-Counts/blob/26c2770b2fd85b96b8984b9c33f79bcafe285898/includes/class-shared-counts-admin.php#L814)
   like so: [https://gist.github.com/billerickson/6463e9660211e056ad3e657c2082f94c](https://gist.github.com/billerickson/6463e9660211e056ad3e657c2082f94c)
    -  This reply was modified 7 years, 1 month ago by [Bill Erickson](https://wordpress.org/support/users/billerickson/).
 *  Thread Starter [sloweye](https://wordpress.org/support/users/sloweye/)
 * (@sloweye)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-11463871)
 * I don’t know why, but the order worked better for me if the ‘NOT EXISTS’ array
   is before the ‘EXISTS’ array.
    -  This reply was modified 7 years, 1 month ago by [sloweye](https://wordpress.org/support/users/sloweye/).
      Reason: typo
 *  Thread Starter [sloweye](https://wordpress.org/support/users/sloweye/)
 * (@sloweye)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-11464294)
 * Here’s a more recent answer in the stackexchange thread I linked to earlier that
   has more about the order of the meta queries: [https://wordpress.stackexchange.com/a/329687/144711](https://wordpress.stackexchange.com/a/329687/144711)
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-11466913)
 * Wow you’re right, I didn’t realize the order would have that effect.
 * Thank you! I’ll get this added to Shared Counts for the next release.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘posts hidden when sorting by shared counts column’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/shared-counts_7188b5.svg)
 * [Shared Counts - Social Media Share Buttons](https://wordpress.org/plugins/shared-counts/)
 * [Support Threads](https://wordpress.org/support/plugin/shared-counts/)
 * [Active Topics](https://wordpress.org/support/plugin/shared-counts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/shared-counts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/shared-counts/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/posts-hidden-when-sorting-by-shared-counts-column/#post-11466913)
 * Status: resolved