My mind is blown that #ChatGPT was able to create a (simple) #WordPress function for me that worked. #Mindblown.
The #prompt
Create a WordPress function that will force tag archives to show custom post types in addition to regular blog posts.
The answer
To force tag archives to display custom post types along with regular blog posts in WordPress, you can use the pre_get_posts action hook to modify the main query. Here’s an example of a WordPress function that achieves this:
function custom_post_type_in_tag_archives($query) {
if (is_tag() && $query->is_main_query()) {
// Add your custom post type slug here
$custom_post_type = 'your_custom_post_type';
// Modify the query to include the custom post type
$query->set('post_type', array('post', $custom_post_type));
}
}
add_action('pre_get_posts', 'custom_post_type_in_tag_archives');
Replace 'your_custom_post_type' with the slug of your custom post type. For example, if your custom post type slug is books, you would replace 'your_custom_post_type' with 'books'.
Once you’ve added this code to your theme’s functions.php file or a custom plugin, the tag archive pages will display both regular blog posts and your specified custom post type.




