Home >CMS Tutorial >WordPress >Customized WordPress Administration Filters

Customized WordPress Administration Filters

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-02-18 11:11:091036browse

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.

Key Concepts

  • WordPress provides default filters on the post administration screen to display posts based on specific criteria. These filters are extensible.
  • New filters are added using the restrict_manage_posts and pre_get_posts hooks. These allow filtering posts by various criteria.
  • Custom filters for post formats and authors can be created by adding code to your theme's functions.php file (or a plugin).
  • The pre_get_posts filter modifies the database query, controlling which posts are displayed.
  • Custom filters improve user experience, add functionality, and maintain clean code.

Filtering Posts: The Basics

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.

Customized WordPress Administration Filters

Creating Custom Filters

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.

Example: Filtering by Author and Post Format

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.

Adding Dropdown Menus (restrict_manage_posts)

Add the following code to your theme's functions.php file (or a plugin):

Filter by Author:

<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>

Filter by Post Format:

<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.

Customized WordPress Administration Filters

Filtering the Post List (pre_get_posts)

Now, let's make the dropdowns functional:

Filtering by Author:

<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>

Filtering by Post Format:

<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.

Customized WordPress Administration Filters

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn