-
locke85
Hi there,
I’m using a Content Template element to display the archive page of a CPT called “news” which has got no category but a standard “tag” (taxonomy) assigned. The URL of a news-post is a follows:
https://domain.de/news/seo-mit-video-modus/
The URL of a tag-archive page looks like that: https://domain.de/tag/funktionsumfang/
Please note that the same standard tags are being used for standard post types as well as CPTs.
Issue: tag-archive pages show all kinds of posts, except news-posts. I checked the CPT.php file (implemented as a plugin), the related entries are there:
taxonomies' => array( 'post_tag' )
-> CPT supports tagshas_archive' => true
-> Archive pages are activatedI found this threat from David which might be related, but I’m not entirely sure.
Any thoughts as to what I’m missing?
Thanks,
Jan -
Alvind
Hi Jan,
Have you tried the snippet provided by David in the topic you shared?
-
locke85
Thanks for getting back, Alvind.
Yes, I added the following to the function.php:
// News CPT - Include Posts in tag archives function db_custom_post_type_for_tags_archive( $query ) { if ( $query->is_main_query() && ! is_admin() && ( is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) { $query->set( 'post_type', array( 'post', 'news' ) ); } } add_action( 'pre_get_posts', 'db_custom_post_type_for_tags_archive' );
Do we need to look into rewrite rules, as the slug /news/ is not part of the tag archive URL?
Any advice is much appreciated.
Best,
Jan -
David
Hi there,
yes, you would need to use rewrite rules or a plugin to change that permalink structure
-
locke85
Hi David,
sorry for not getting back earlier.
Meanwhile I added the following rewrite rule to the php-file of the plugin:
// Custom Rewrite Rule for Tags in 'news' CPT function news_tag_rewrite_rules($rules) { $new_rules = array( 'tag/([^/]+)/?$' => 'index.php?post_type=news&taxonomy=post_tag&tag=$matches[1]', ); return $new_rules + $rules; } add_filter('rewrite_rules_array', 'news_tag_rewrite_rules');
Then I…
(1) removed the
db_custom_post_type_for_tags_archive
function from the function.php
(2) regenerated the rewrite rules (by saving the permalinks) and
(3) cleared the server cache.The tag achieves now are also show CPT posts.
Thanks for your valuable support.
Best,
Jan -
Glad to hear you got it working!
- You must be logged in to reply to this topic.