Add custom filters for post types in WordPress

Creating and using custom fields in post types is something very essential these days. To make this more effective we need to show these custom fields in the archive page in wp-admin. We have discussed this in a previous post.

For each certain custom columns, it would be good to have filters too. For example, If we have a custom post type called “contact” and we have configured custom column “area”, then it would be awesome to filter contacts by area.

Note: It is NOT mandatory to have a filter ONLY when there is a custom column for that field. It is just thinking logically.

Ok, lets get started. First we need to create a custom filter input / select box.

Now, we need to add logic to filter results based on the selection made by user in our custom filter. Here is the hook to parse the query during filter.

Note: In this article we have explained how to filter results by custom field of a post type. Sometimes you need to manually filter results by custom date field. You need to modify your code accordingly. You need to take a look at meta_query and DATETIME type in it.

Share this post