Adding tags to WordPress pages
I try to display both posts and pages that are tagged with the same tag when I'm on a WordPress tag page. I'm using some custom code to be able to add the tag functionality to WordPress pages. Adding a tag to a page works and I can also display the tag on the page when I'm using <?php the_tags(); />
in a page template.
I'm using this code (in my child theme functions.php) to register the tag functionality to pages:
// Add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'tags_support_all');
The issue - Pages with tags are not being displayed on the tag page
I can't seem to find a way to get these pages displayed on the tag page. It only shows the posts that are tagged with the same tag. No pages. I use the code below to try and update the pre_get_posts query by setting the post_type to 'any'. It's an (old) snippet that I found on Google why I was searching for a solution. I'm not sure if this is still the way to go, but I couldn't find any working alternatives.
// Display all post types on tag pages
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) {
$wp_query->set('post_type', 'any');
}
}
add_action('pre_get_posts', 'tags_support_query');
Any ideas on how to get this working?
Some extra information:
- WordPress version 5.7.2.
- I'm using a theme and (custom) child theme combination. The theme uses the Gutenberg editor.
- The above code snippets are added to the child theme functions.php
- There's no specific tag.php or archive.php. The tag page uses the default index.php to display the tag page. This index.php is also used on categories, archives, etc.
index.php
regarding the output of the posts + pages. - Emiel Zuurbier