As it has been requested, here is our clean and simple solution to adding a Show All button for taxonomy filters.
Steps:
- For the built-in taxonomies (such as Category), you can rename the default term name to “Show All” and the slug to “all”. When creating a custom taxonomy you want to have a Show All button for, add a default term. In ACF this can be done using the following setting:
- Then add the following code snippet - change
$post_types
and$taxonomies
to match your requirements:
function auto_add_post_taxonomy_term( $post_ID ) {
$post_types = ['article']; // The post types you want to apply this to
$taxonomies = ['article-category', 'industry', 'region']; // The taxonomies to display Show All for
$current_post_type = get_post_type($post_ID);
$term_slug = 'all';
if(in_array($current_post_type, $post_types)) {
foreach ($taxonomies as $taxonomy) {
$term_obj = get_term_by('slug', $term_slug, $taxonomy);
$post_terms = wp_get_object_terms( $post_ID, $taxonomy );
if (!in_array($term_obj, $post_terms)) {
wp_add_object_terms($post_ID, $term_slug, $taxonomy);
}
}
}
return $post_ID;
}
add_action( 'save_post', 'auto_add_post_taxonomy_term' );
Important notes:
- If you have multiple post types and want different taxonomies to apply to each of them, you will have to adjust the code snippet accordingly.
- To add the code snippet please see the following tip:
I hope this saves you some time and helps you to do this more easily.