How to add filter posts by date in admin WordPress, help you save time and improve the efficiency of content management on your website.
How to add filter Posts by Date in Admin WordPress
Are you managing content on WordPress and having trouble finding and sorting posts by date? Don’t worry, this article will help you add a post filter by date in WordPress Admin, making it easier than ever to sort posts by day and month. Let’s learn how to do it with ItsmeIT!
Add Post Filter by Day and Month in WordPress with Code
If you prefer to use lines of code without using a plugin, you can copy and add the following to functions.php in your theme. This way you can use the functions.php file directly or use the Code Snippets plugin.
The code to filter posts by date in Admin Wordpress will be as follows:
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();
The above code uses jQuery UI datepicker to display the date fields and uses the method pre_get_posts
to filter posts by date in Wordpress.
Filter Posts in WordPress Admin by Date Range using a Plugin
This method is simpler, you just need to download the plugin “Filer Post By Date”, then install and activate it, this way you will not need to write code. This is a plugin from ItsmeIT that has been used and re-shared for everyone.
- Step 1: Go to this link to download the plugin.
- Step 2: Click on “Code -> Download ZIP”.
- Step 3: Rename the folder “filter-post-date-main” to “filter-post-date” and upload it to the wp-content->plugin folder
- Step 4: Log in to Admin page and navigate to Plugins -> search for “Filer Post By Date” and activate.
- Step 5: Now you can filter posts or photos, media by day or month in Admin Wordpress.
Another option is to download the plugin from the link provided below and install it by going to Plugins, then Add New and Upload Plugin. Choose the downloaded zip file, install it and finally activate it. With this plugin, you can easily filter posts by Date in Admin WordPress.
– Advertising –