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.
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 usingposts_where
.The row
All | Published | ...
is controlled byviews_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:
The link contains
meta_data=allorany
(the custom field name), which will be used to filter by meta key.And then, filter when needed:
With credit to @brasofilo for his answer, it gave me all kinds of problems for a reason I couldn’t quite diagnose. I think the posts_where filter that gets added needs to be removed otherwise it will run for all future queries in the page and make a mess of anything that then tries to query a different post_type as the second filter will still be active.
Here’s a solution that doesn’t require hacking the SQL manually, but works on the Query object instead. Assuming using the method of adding URL params as in the other answer here with url param name cpt_filter
No problems here with adding/removing filters, and flexibility with setting the meta key and value to whatever works.