Removing ?post_type=foo from the url for custom taxonomies
-
If you create a custom taxonomy which applies to precisely one custom post type, you can bypass the need for
?post_type=foo
in your permalinks using the following code.if (!function_exists('chmac_pre_get_posts')): function chmac_pre_get_posts($query) { if (is_main_query() && !is_admin() && !empty($query->query_vars['CUSTOM_TAXONOMY'])) $query->query_vars['post_type'] = $query->query['post_type'] = 'CUSTOM_POST_TYPE'; } endif; add_filter('pre_get_posts', 'chmac_pre_get_posts');
I spent an hour or two looking for this and ended up writing the code in a few minutes. Hopefully this saves somebody else the hassle.
Love & joy – Callum.
PS> Naturally you need to replace CUSTOM_TAXONOMY and CUSTOM_POST_TYPE with appropriate values (in lowercase).
- The topic ‘Removing ?post_type=foo from the url for custom taxonomies’ is closed to new replies.