Home >CMS Tutorial >WordPress >Customized WordPress Administration Filters
This article explains how to use WordPress filters to customize the post administration screen. We'll cover WordPress's built-in filters and show you how to create custom filters to enhance functionality.
restrict_manage_posts
and pre_get_posts
hooks. These allow filtering posts by various criteria.functions.php
file (or a plugin).pre_get_posts
filter modifies the database query, controlling which posts are displayed.WordPress offers default filters on the post administration screen (like date filtering, shown below). Themes and plugins can add more. These filters narrow down the post list based on specified criteria.
While WordPress offers built-in filters, you often need custom ones for better user experience. Two hooks are key:
restrict_manage_posts
: Adds new filter controls to the admin screen's top.pre_get_posts
: Modifies the query before it runs, filtering the displayed posts.Let's create filters for post authors and formats. Imagine a website where posts have manually assigned formats and authors. The default admin screen can be overwhelming. We'll add dropdown menus for easier filtering.
restrict_manage_posts
)Add the following code to your theme's functions.php
file (or a plugin):
<code class="language-php">function add_author_filter() { global $post_type; if ($post_type == 'post') { $user_args = array( 'show_option_all' => 'All Authors', 'orderby' => 'display_name', 'order' => 'ASC', 'name' => 'author_filter', 'who' => 'authors', 'include_selected' => true ); if (isset($_GET['author_filter'])) { $user_args['selected'] = (int) sanitize_text_field($_GET['author_filter']); } wp_dropdown_users($user_args); } } add_action('restrict_manage_posts', 'add_author_filter');</code>
<code class="language-php">function add_post_format_filter() { global $post_type; if ($post_type == 'post') { $post_formats_args = array( 'show_option_all' => 'All Formats', 'orderby' => 'NAME', 'order' => 'ASC', 'name' => 'post_format_filter', 'taxonomy' => 'post_format' ); if (isset($_GET['post_format_filter'])) { $post_formats_args['selected'] = sanitize_text_field($_GET['post_format_filter']); } wp_dropdown_categories($post_formats_args); } } add_action('restrict_manage_posts', 'add_post_format_filter');</code>
This adds two dropdowns to the post list screen.
pre_get_posts
)Now, let's make the dropdowns functional:
<code class="language-php">function filter_posts_by_author($query) { global $post_type, $pagenow; if ($pagenow == 'edit.php' && $post_type == 'post' && isset($_GET['author_filter'])) { $author_id = sanitize_text_field($_GET['author_filter']); if ($author_id != 0) { $query->set('author', $author_id); } } } add_action('pre_get_posts', 'filter_posts_by_author');</code>
<code class="language-php">function filter_posts_by_format($query) { global $post_type, $pagenow; if ($pagenow == 'edit.php' && $post_type == 'post' && isset($_GET['post_format_filter'])) { $post_format = sanitize_text_field($_GET['post_format_filter']); if ($post_format != 0) { $query->set('post_format', $post_format); } } } add_action('pre_get_posts', 'filter_posts_by_format');</code>
These functions modify the query to only include posts matching the selected author or post format.
This enhanced your WordPress admin with custom filters. You can adapt this to filter by other post attributes (refer to the WordPress Query class documentation). Remember to always sanitize user inputs to prevent security vulnerabilities.
The above is the detailed content of Customized WordPress Administration Filters. For more information, please follow other related articles on the PHP Chinese website!