Title: Sortable Custom Column?
Last modified: June 29, 2021

---

# Sortable Custom Column?

 *  [otto2021](https://wordpress.org/support/users/otto2021/)
 * (@otto2021)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/sortable-custom-column/)
 * I’m sorry, I speak a little English.
 * My MySQL table:
 *     ```
       CREATE TABLE wp_visitors (
   
           id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
           post_id INT NOT NULL
   
       );
       ```
   
 * The ~/wp-content/themes/mycustomtheme/functions.php file:
 * Add a custom column:
 *     ```
       function add_sticky_column ( $columns ) {
   
           return array_merge ( $columns, array ( 'sticky'=> 'Visitors' ) );
   
       }
   
       add_filter ( 'manage_posts_columns' , 'add_sticky_column' );
       ```
   
 * The “Visitors” custom column values:
 *     ```
       function display_posts_stickiness ( $column, $post_id ) {
   
           if ( $column == 'sticky' ) {
   
               echo custom_counter ( $post_id );
   
           } else {}
   
       }
   
       add_action ( 'manage_posts_custom_column' , 'display_posts_stickiness', 10, 2 );
       ```
   
 * The custom_counter function:
 *     ```
       function custom_counter ( $post_id ) {
   
           global $wpdb;
   
           $prefix = $wpdb-> prefix; // wp_
   
           $mysqli = new mysqli ( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
   
           $sql = "SELECT * FROM " . $prefix . "visitors WHERE post_id=" . $post_id;
   
           $result = $mysqli-> query ( $sql );
   
           $row_cnt = $result-> num_rows;
   
           $mysqli-> close ();
   
           return $row_cnt;
   
       }
       ```
   
 * Sorting:
 *     ```
       function ws_sortable_manufacturer_column ( $columns ) {
   
           $columns [ 'sticky' ] = 'Visitors';
   
           return $columns;
   
       }
   
       add_filter ( 'manage_edit-post_sortable_columns', 'ws_sortable_manufacturer_column' );
       ```
   
 * It works! Need help for:
 *     ```
       function ws_orderby_custom_column ( $query ) {
   
           global $pagenow;
   
           if ( ! is_admin () || 'edit.php' != $pagenow || ! $query-> is_main_query () || 'post' != $query-> get ( 'post_type' ) ) {
   
               return;
   
           } else {}
   
           $orderby = $query-> get ( 'orderby' );
   
           switch ( $orderby ) {
   
               case 'Visitors':
   
                   // please help me!
   
                   break;
   
               default:
   
                   break;
   
           }
   
       }
   
       add_action ( 'pre_get_posts', 'ws_orderby_custom_column' );
       ```
   
 * Please help me. Thanks.
    -  This topic was modified 4 years, 11 months ago by [otto2021](https://wordpress.org/support/users/otto2021/).

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

 *  Thread Starter [otto2021](https://wordpress.org/support/users/otto2021/)
 * (@otto2021)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/sortable-custom-column/#post-14609370)
 * Deleted.
    -  This reply was modified 4 years, 11 months ago by [otto2021](https://wordpress.org/support/users/otto2021/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/sortable-custom-column/#post-14611843)
 * “pre_get_posts” belongs to `WP_Query` class. It’s impractical to sort by custom
   values in a custom table using this class because the class is unaware of such
   a table and hence there are no possible query vars to set.
 * What you could do is use the “posts_request” filter (part of the same class and
   method) to replace the default SQL created by the class with your own custom 
   SQL that joins in your table so that the your table’s column can be used for 
   ordering of the results.

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

The topic ‘Sortable Custom Column?’ is closed to new replies.

## Tags

 * [column](https://wordpress.org/support/topic-tag/column/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [development](https://wordpress.org/support/topic-tag/development/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [4 years, 11 months ago](https://wordpress.org/support/topic/sortable-custom-column/#post-14611843)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
