conditional tags - Filter for the Custom Post List Page

I'm adding a filter on my custom post type 'book' list page for easier searching. I've found the rel

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745083158a4610246.html

相关推荐

  • conditional tags - Filter for the Custom Post List Page

    I'm adding a filter on my custom post type 'book' list page for easier searching. I've found the rel

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信