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.
![Instructions on how to filter posts by date in Admin Wordpress 17 How to add filter Posts by Date in Admin WordPress (illustration)](https://arriveddev.com/wp-content/uploads/2023/04/how-to-add-filter-posts-by-date-in-admin-wordpress-1024x631.jpg)
Add Post Filter by date in WordPress Using plugin
- Step 1: Download the “Filter Post By Date” plugin.
- Step 2: Go to WordPress Admin > Plugins > Add New > Upload Plugin. Choose the downloaded zip file, install, and activate it.
- Step 3: Navigate to Posts or Media in your WordPress Admin.
- Step 4: You will now see new date filter options above your post or media list.
- Step 5: Use these options to filter posts, photos, or other media by day or month.
With this plugin installed and activated, you can easily filter content by date in the WordPress Admin panel, improving your content management efficiency.
Add Post Filter by Day and Month in WordPress with Code
To implement this feature without a plugin, add the following code to your theme’s functions.php
file 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.
– Advertising –