admin管理员组文章数量:1298182
I'm trying to get my Custom Post Type to show up in the main archive using pre_get_posts, but it just won't work.
However, in the search results the entries are displayed, and they are also displayed within an assigned category.
The code I use:
function include_custom_post_type_archives($query) {
if ((is_category() || is_tag()) && $query->is_archive() && empty($query->query_vars['suppress_filters'])) {
$query->set('post_type', array('post', 'landingpages'));
}
return $query;
}
add_filter('pre_get_posts', 'include_custom_post_type_archives');
I'm trying to get my Custom Post Type to show up in the main archive using pre_get_posts, but it just won't work.
However, in the search results the entries are displayed, and they are also displayed within an assigned category.
The code I use:
function include_custom_post_type_archives($query) {
if ((is_category() || is_tag()) && $query->is_archive() && empty($query->query_vars['suppress_filters'])) {
$query->set('post_type', array('post', 'landingpages'));
}
return $query;
}
add_filter('pre_get_posts', 'include_custom_post_type_archives');
Share
Improve this question
edited Sep 4, 2021 at 18:48
anyway
asked Sep 3, 2021 at 4:37
anywayanyway
134 bronze badges
1
|
1 Answer
Reset to default 1Okay, this is what works for me, thanks to the friendly hint of Jacob Peattie:
function include_custom_post_type_archives($query)
{
if (is_home() && empty($query->query_vars['suppress_filters'])) {
$query->set('post_type', array(
'post', 'landingpages',
));
return $query;
}
}
add_filter('pre_get_posts', 'include_custom_post_type_archives');
本文标签: custom post typesCPT is simply not displayed in the main archive with quotpregetpostsquot
版权声明:本文标题:custom post types - CPT is simply not displayed in the main archive with "pre_get_posts" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741333554a2372906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_main_query()
does not refer to the "main archive". It refers to the primary query for content for every view. On a single page the main query is the query for that page, on a date archive it's the query for posts from that date, and on taxonomy archives it's the query for posts in that taxonomy term. If you want to apply the filter to the blog query then you need to use$query->is_home()
. – Jacob Peattie Commented Sep 3, 2021 at 9:55