最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

conditional tags - Filter for the Custom Post List Page

programmeradmin0浏览0评论

I'm adding a filter on my custom post type 'book' list page for easier searching. I've found the relevant code and everything is good except that the filter not only appeared on the 'book' list page, but also on other list pages, for example, the post list page. I've reviewed the code but am still clueless.

<?php
add_action( 'restrict_manage_posts', 'book_genre_filter_page' );

function book_genre_filter_page(){
$type = 'book';

if ('book' == $type){

    $values = array(
        'Science Fiction' => 'sf', 
        'Autobiography' => 'ab',
        'Thriller' => 't',
    );
    ?>
    <select name="genre">
    <option value=""><?php _e('All genres', 'book_genre'); ?></option>
    <?php
        $current_v = isset($_GET['genre'])? $_GET['genre']:'';
        foreach ($values as $label => $value) {
            printf
                (
                    '<option value="%s"%s>%s</option>',
                    $value,
                    $value == $current_v? ' selected="selected"':'',
                    $label
                );
            }
    ?>
    </select>
    <?php
}
}


add_filter( 'parse_query', 'book_genre_filter' );

function book_genre_filter( $query ){
global $pagenow;
$type = $query->query['post_type'];
    $target = 'book';

    if ( $type == $target ) {
        if ( $pagenow =='edit.php' && isset($_GET['genre']) && $_GET['genre'] != '') {
                $query->query_vars['meta_key'] = 'genre';
                $query->query_vars['meta_value'] = $_GET['genre']; 
        }
    }
}

I'm adding a filter on my custom post type 'book' list page for easier searching. I've found the relevant code and everything is good except that the filter not only appeared on the 'book' list page, but also on other list pages, for example, the post list page. I've reviewed the code but am still clueless.

<?php
add_action( 'restrict_manage_posts', 'book_genre_filter_page' );

function book_genre_filter_page(){
$type = 'book';

if ('book' == $type){

    $values = array(
        'Science Fiction' => 'sf', 
        'Autobiography' => 'ab',
        'Thriller' => 't',
    );
    ?>
    <select name="genre">
    <option value=""><?php _e('All genres', 'book_genre'); ?></option>
    <?php
        $current_v = isset($_GET['genre'])? $_GET['genre']:'';
        foreach ($values as $label => $value) {
            printf
                (
                    '<option value="%s"%s>%s</option>',
                    $value,
                    $value == $current_v? ' selected="selected"':'',
                    $label
                );
            }
    ?>
    </select>
    <?php
}
}


add_filter( 'parse_query', 'book_genre_filter' );

function book_genre_filter( $query ){
global $pagenow;
$type = $query->query['post_type'];
    $target = 'book';

    if ( $type == $target ) {
        if ( $pagenow =='edit.php' && isset($_GET['genre']) && $_GET['genre'] != '') {
                $query->query_vars['meta_key'] = 'genre';
                $query->query_vars['meta_value'] = $_GET['genre']; 
        }
    }
}
Share Improve this question edited Oct 14, 2019 at 7:48 Max Yudin 6,3882 gold badges26 silver badges36 bronze badges asked Oct 14, 2019 at 1:34 Gary HuGary Hu 253 bronze badges 1
  • There are conditional tags to figure out what page you are on. – Max Yudin Commented Oct 14, 2019 at 7:41
Add a comment  | 

1 Answer 1

Reset to default 1

The restrict_manage_posts hook supplies the current post type as the very first parameter to the callback function (which is book_genre_filter_page() in your code), so you should use it (instead of the static $type variable in your code) to determine the current post type or to ensure your custom filter is displayed only on the admin screen for editing posts in your post type:

add_action( 'restrict_manage_posts', 'book_genre_filter_page' );
function book_genre_filter_page( $post_type ) {
    if ( 'book' == $post_type ) {
        ... your code here ...
    }
}
发布评论

评论列表(0)

  1. 暂无评论