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.
Topic columns theme-development sort Wordpress
Category Web