本文介绍了作者& 联合检索自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过与我类似的问题,但没有找到我需要的答案.

I have searched questions similar to mine but with no luck finding the answer I need.

我有作者和自定义帖子类型 (CPT).我的搜索结果已经显示了所有 CPT——但是,此外,我还需要一些比这更具体的内容.我需要我的搜索功能来允许对特定作者和特定 CPT 的组合查询.例如,阿尔伯特·爱因斯坦的所有博客.

I have Authors and I have Custom Post Types (CPT). My search results already display all CPT's -- but, additionally, I need something more specific than that. I need my search function to allow combined queries for a specific Author and specific CPT. For example, all Blogs by Albert Einstein.

这个网址/?s=%20&author_name=alberteinstein"返回阿尔伯特·爱因斯坦在 CPT 上的所有帖子.

This url "/?s=%20&author_name=alberteinstein" returns all posts across CPT's by Albert Einstein.

但是,如果我为完整网址添加&post_type=blogs"以过滤 CPT,如下所示:

But if I add "&post_type=blogs" for the full url to filter for the CPT like this:

/?s=%20&author_name=alberteinstein&post_type=blogs"

"/?s=%20&author_name=alberteinstein&post_type=blogs"

它不只过滤博客——它仍然返回作者的所有 CPT,与上面相同.

it does not filter for just Blogs -- it still returns all CPT's by the Author, same as above.

我需要能够查询作者和特定 CPT.

I need to be able to query for an Author and specific CPT.

这让我疯狂了好几个星期.任何帮助将不胜感激.

This has been driving me crazy for weeks. Any help would be greatly appreciated.

推荐答案

这可能会有所帮助(如 WordPress Codex 中所述帖子类型页面).基本上,可能是您的自定义帖子类型 (CPT) 未注册用于存档查询,尽管它已合法注册用作 CPT.

This may help (as worded on the WordPress Codex post types page). Basically, it may be that your custom post type (CPT) isn't registered for archive queries although it is legitimately registered for use as a CPT.

注册自定义帖子类型并不意味着它会自动添加到主查询中.如果您希望自定义帖子类型的帖子显示在标准档案中或将它们与其他帖子类型混合包含在主页上,请使用 pre_get_posts 操作挂钩.

Registering a custom post type does not mean it gets added to the main query automatically. If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.

// Show posts of 'post', 'page' and 'movie' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'movie' ) );
    return $query;
}

这篇关于作者& 联合检索自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:26