Skip to content

Instantly share code, notes, and snippets.

@kirasiris
Created February 3, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirasiris/bf2a4dd5ac044ea2249a21d400f33e27 to your computer and use it in GitHub Desktop.
Save kirasiris/bf2a4dd5ac044ea2249a21d400f33e27 to your computer and use it in GitHub Desktop.
<?php
// Filtrar Post por Date Range
class kuafDateRange{
function __construct(){
// if you do not want to remove default "by month filter", remove/comment this line
add_filter( 'months_dropdown_results', '__return_empty_array' );
// include CSS/JS, in our case jQuery UI datepicker
add_action( 'admin_enqueue_scripts', array( $this, 'jqueryui' ) );
// HTML of the filter
add_action( 'restrict_manage_posts', array( $this, 'form' ) );
// the function that filters posts
add_action( 'pre_get_posts', array( $this, 'filterquery' ) );
}
/*
* Add jQuery UI CSS and the datepicker script
* Everything else should be already included in /wp-admin/ like jquery, jquery-ui-core etc
* If you use WooCommerce, you can skip this function completely
*/
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' );
}
/*
* Two input fields with CSS/JS
* If you would like to move CSS and JavaScript to the external file - welcome.
*/
function form(){
$from = ( isset( $_GET['kuafDateFrom'] ) && $_GET['kuafDateFrom'] ) ? $_GET['kuafDateFrom'] : '';
$to = ( isset( $_GET['kuafDateTo'] ) && $_GET['kuafDateTo'] ) ? $_GET['kuafDateTo'] : '';
echo '<style>
input[name="kuafDateFrom"], input[name="kuafDateTo"]{
line-height: 28px;
height: 28px;
margin: 0;
width:125px;
}
</style>
<input type="text" name="kuafDateFrom" placeholder="Date From" value="' . $from . '" />
<input type="text" name="kuafDateTo" placeholder="Date To" value="' . $to . '" />
<script>
jQuery( function($) {
var from = $(\'input[name="kuafDateFrom"]\'),
to = $(\'input[name="kuafDateTo"]\');
$( \'input[name="kuafDateFrom"], input[name="kuafDateTo"]\' ).datepicker();
// by default, the dates look like this "April 3, 2017" but you can use any strtotime()-acceptable date format
// to make it 2017-04-03, add this - datepicker({dateFormat : "yy-mm-dd"});
// the rest part of the script prevents from choosing incorrect date interval
from.on( \'change\', function() {
to.datepicker( \'option\', \'minDate\', from.val() );
});
to.on( \'change\', function() {
from.datepicker( \'option\', \'maxDate\', to.val() );
});
});
</script>';
}
/*
* The main function that actually filters the posts
*/
function filterquery( $admin_query ){
global $pagenow;
if (
is_admin()
&& $admin_query->is_main_query()
// by default filter will be added to all post types, you can operate with $_GET['post_type'] to restrict it for some types
&& in_array( $pagenow, array( 'edit.php', 'upload.php' ) )
&& ( ! empty( $_GET['kuafDateFrom'] ) || ! empty( $_GET['kuafDateTo'] ) )
) {
$admin_query->set(
'date_query', // I love date_query appeared in WordPress 3.7!
array(
'after' => $_GET['kuafDateFrom'], // any strtotime()-acceptable format!
'before' => $_GET['kuafDateTo'],
'inclusive' => true, // include the selected days as well
'column' => 'post_date' // 'post_modified', 'post_date_gmt', 'post_modified_gmt'
)
);
}
return $admin_query;
}
}
new kuafDateRange();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment