I have a custom post type that has custom meta that I want to be able to filter on the Admin page. Much like the "All | Published | Drafts | and Trash" links located above the post list. I can't seem to find a hook to hook into. Does one exist?

This question isn't exactly what I am asking. I would prefer not to have the filter dropdown, but instead a link like …

All | Published | Drafts | Trash

… across the top of the editor.

  • 3
    check the first question in the related column over there ->
    – Milo
    Apr 4 '13 at 21:48
  • 1
    I have seen this, but what I was asking is can you add a link to the list similar to the "All|Published|Drafts|Trash" links. My client finds the drop down a bit confusing, and would prefer a simple click a link setup.
    – micahmills
    Apr 5 '13 at 3:28

Just like in Adding a Taxonomy Filter to Admin List for a Custom Post Type? the filter parse_query could be used, but here I'm using posts_where.

The row All | Published | ... is controlled by views_edit-{$post_type} and the $views array contains each item that's a simple anchor tag.

First, we insert a couple of links - a separator and a Meta filter:

add_filter( 'views_edit-portfolio', 'meta_views_wpse_94630', 10, 1 );

function meta_views_wpse_94630( $views ) 
    $views['separator'] = '           ';
    $views['metakey'] = '<a href="edit.php?meta_data=allorany&post_type=portfolio">Meta Key</a>';
    return $views;

The link contains meta_data=allorany (the custom field name), which will be used to filter by meta key.

enter image description here

And then, filter when needed:

add_action( 'load-edit.php', 'load_custom_filter_wpse_94630' );

function load_custom_filter_wpse_94630()
    global $typenow;

    // Adjust the Post Type
    if( 'portfolio' != $typenow )

    add_filter( 'posts_where' , 'posts_where_wpse_94630' );

function posts_where_wpse_94630( $where ) 
    global $wpdb;       
    if ( isset( $_GET[ 'meta_data' ] ) && !empty( $_GET[ 'meta_data' ] ) ) 
        $meta = esc_sql( $_GET['meta_data'] );
        $where .= " AND ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key='$meta' )";
    return $where;
  • 1
    +1, this is slick. Though I do feel that somebody having "clients" ought to be able to modify Mike Schinkel's answer in the near-duplicate to their needs... Apr 5 '13 at 14:28
  • 1
    Thanks for this answer it was incredibly helpful. For whatever reason I missed the views_edit-{$post_type} and $views thing. @Johannes Thank you for your piety, I will be sure to point all my clients in your direction in the future.
    – micahmills
    Apr 5 '13 at 15:59
  • While probably still true, that might have been a tid bit of a snooty remark @micahmills, I'll give you that. Apr 5 '13 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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