1

This is my snippet for drop-down filter in custom posts types:

<?php        
        function blog_restrict_manage_posts() {
        global $typenow;
        $taxonomy = 'blog'; // Change this
        if( $typenow != "page" && $typenow != "post" ){
            $filters = array($taxonomy);
            foreach ($filters as $tax_slug) {
                $tax_obj = get_taxonomy($tax_slug);
                $tax_name = $tax_obj->labels->name;
                $terms = get_terms($tax_slug);
                echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
                echo "<option value=''>Wszystkie $tax_name</option>";
                foreach ($terms as $term) { 
                    $label = (isset($_GET[$tax_slug])) ? $_GET[$tax_slug] : ''; // Fix
                    echo '<option value='. $term->slug, $label == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
                }
                echo "</select>";
            }
        }
    }
    add_action( 'restrict_manage_posts', 'blog_restrict_manage_posts' );
?>

But there is one small issue - I have a two custom posts: 'x' and 'y'. And I have this drop-down menu filter. But filter for 'x' is visible in custom posts 'y' admin page and vice versa. So, every drop-downs are visible in every custom posts admin pages...

Thanks :)

2
  • Can you please elaborate more on your question? What is the issue? What have you tried to fix it? Jul 30, 2017 at 23:15
  • I do not know how to fix it, even to start :D My issue is: I have a two custom posts: 'x' and 'y'. And I have this dropdown menu filter. But filter for 'x' is visible in custom posts 'y' admin page and vice versa. So, every dropdowns are visible in every custom posts admin pages...
    – D_P
    Jul 30, 2017 at 23:18

1 Answer 1

2

When using this hook, your function receives two parameters, onde of them is the slug of the current post_type;

<?php
function my_post_type_filter($post_type) {

  $post_slug = $post_type; 

  if( $post_slug == 'the_post_type_slug') {
    // Do something
  }
}


add_action( 'restrict_manage_posts', 'my_post_type_filter' );

?>

To learn more about this hook check the Docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.