-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter-post-date.php
More file actions
97 lines (80 loc) · 2.84 KB
/
filter-post-date.php
File metadata and controls
97 lines (80 loc) · 2.84 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
Plugin Name: Filer Post By Date
Plugin URI: https://itsmeit.co/
Description: Filter posts or Media by day or month in Admin management page.
Version: 1.0.0
Author: itsmeit.co
Author URI: https://itsmeit.co/
Network: true
Text Domain: filter-post-date
Copyright 2023 itsmeit.co (email: buivanloi.2010@gmail.com)
*/
class FilterPostByDate
{
public function __construct()
{
add_action('init', array($this, 'initFilterPostByDateAdmin'), 99);
}
public function initFilterPostByDateAdmin()
{
global $pagenow;
if (is_admin() && in_array($pagenow, array('edit.php', 'upload.php'))) {
add_filter('months_dropdown_results', '__return_empty_array');
add_action('admin_enqueue_scripts', array($this, 'jqueryui'));
add_action('restrict_manage_posts', array($this, 'form'));
add_action('pre_get_posts', array($this, 'filterQuery'));
}
}
public function jqueryui()
{
wp_enqueue_style('jquery-ui', '//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css');
wp_enqueue_script('jquery-ui-datepicker');
}
public function form()
{
$from = (isset($_GET['itsmeitDateFrom']) && $_GET['itsmeitDateFrom']) ? $_GET['itsmeitDateFrom'] : '';
$to = (isset($_GET['itsmeitDateTo']) && $_GET['itsmeitDateTo']) ? $_GET['itsmeitDateTo'] : '';
echo '<style>
input[name="itsmeitDateFrom"], input[name="itsmeitDateTo"]{
line-height: 28px;
height: 28px;
margin: 0;
width:125px;
}
</style>
<input type="text" name="itsmeitDateFrom" placeholder="Date From" value="' . esc_attr($from) . '" />
<input type="text" name="itsmeitDateTo" placeholder="Date To" value="' . esc_attr($to) . '" />
<script>
jQuery( function($) {
var from = $(\'input[name="itsmeitDateFrom"]\'),
to = $(\'input[name="itsmeitDateTo"]\');
$( \'input[name="itsmeitDateFrom"], input[name="itsmeitDateTo"]\' ).datepicker( {dateFormat : "yy-mm-dd"} );
from.on( \'change\', function() {
to.datepicker( \'option\', \'minDate\', from.val() );
});
to.on( \'change\', function() {
from.datepicker( \'option\', \'maxDate\', to.val() );
});
});
</script>';
}
public function filterQuery($admin_query)
{
if ($admin_query->is_main_query()
&& (!empty($_GET['itsmeitDateFrom']) || !empty($_GET['itsmeitDateTo']))
) {
$admin_query->set(
'date_query',
array(
'after' => sanitize_text_field($_GET['itsmeitDateFrom']), // any strtotime()-acceptable format!
'before' => sanitize_text_field($_GET['itsmeitDateTo']),
'inclusive' => true,
'column' => 'post_date'
)
);
}
return $admin_query;
}
}
new FilterPostByDate();