-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin-columns.php
More file actions
72 lines (66 loc) · 1.9 KB
/
admin-columns.php
File metadata and controls
72 lines (66 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* CloudScale Analytics - Admin Columns
*
* Adds a sortable Views column to the Posts list table in wp-admin.
*
* @package CloudScale_Free_Analytics
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'manage_posts_columns', 'cspv_add_admin_column' );
add_action( 'manage_posts_custom_column', 'cspv_render_admin_column', 10, 2 );
add_filter( 'manage_edit-post_sortable_columns', 'cspv_sortable_column' );
add_action( 'pre_get_posts', 'cspv_sort_by_views' );
/**
* Add the Views column to the Posts list table.
*
* @since 1.0.0
* @param array $columns Existing column headers.
* @return array Modified column headers.
*/
function cspv_add_admin_column( $columns ) {
$columns['cspv_views'] = __( '👁 Views', 'cloudscale-wordpress-free-analytics' );
return $columns;
}
/**
* Render the Views column value for a given post.
*
* @since 1.0.0
* @param string $column Column slug being rendered.
* @param int $post_id Current post ID.
* @return void
*/
function cspv_render_admin_column( $column, $post_id ) {
if ( 'cspv_views' === $column ) {
echo esc_html( number_format( cspv_get_view_count( $post_id ) ) );
}
}
/**
* Register the Views column as sortable.
*
* @since 1.0.0
* @param array $columns Existing sortable columns.
* @return array Modified sortable columns.
*/
function cspv_sortable_column( $columns ) {
$columns['cspv_views'] = 'cspv_views';
return $columns;
}
/**
* Apply the Views column sort to the main admin query.
*
* @since 1.0.0
* @param WP_Query $query The current WP_Query instance.
* @return void
*/
function cspv_sort_by_views( WP_Query $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'cspv_views' === $query->get( 'orderby' ) ) {
$query->set( 'meta_key', CSPV_META_KEY );
$query->set( 'orderby', 'meta_value_num' );
}
}