【问题标题】:query_posts with custom taxonomy具有自定义分类的 query_posts
【发布时间】:2021-04-28 15:18:32
【问题描述】:

这是为了创建一个自定义的 RSS 提要。我有一个名为quotes 的CPT,带有一个名为quote_category 的分类法

我想要所有 ID 为 115 的 quote_category = 'Dogs' 的帖子。这不起作用:

$postCount = 10; // The number of posts to show in the feed
$postType = 'quotes'; // post type to display in the feed
$catName = 'Dogs';
$posts = query_posts(array('post_type'=>$postType, 'quote_category'=>$catName, 'showposts' => $postCount));

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    一般来说,consensusavoid query_posts 不惜一切代价。 official documentation 甚至支持这一点:

    注意:此函数将完全覆盖主查询,不适用于插件或主题。其修改主查询的过于简单的方法可能会产生问题,应尽可能避免使用。在大多数情况下,有更好、更高性能的选项来修改主查询,例如通过 WP_Query 中的“pre_get_posts”操作。

    相反,get_posts() 将执行您希望它执行的所有操作。对于您的情况,您只需在tax_query 中添加您的信息。

    $posts = get_posts(
        [
            'post_type' => 'quotes',
            'numberposts' => 10,
            'tax_query' => [
                [
                    'taxonomy' => 'quote_category',
                    'field' => 'name',
                    'terms' => 'Dogs',
                ],
            ],
        ]
    )
    

    如果您需要多个,terms 参数也接受一个数组:

                    'terms' => ['Dogs', 'Cats'],
    

    编辑

    如果您使用的是get_template_part,那么您可能也在使用标准的 WordPress 循环。这实际上是您可以使用query_posts 的少数几次之一,但我个人仍然不使用也不推荐它。在理想情况下,您可能会像pre_get_posts 一样使用过滤器,但不幸的是我没有时间编写代码来测试它。您应该可以使用以下代码。

    此代码是一个调用匿名函数的操作,该函数使用另一个匿名函数作为回调来添加提要。您的代码执行完全相同的操作,但是您使用的是 100% 正确、安全且常见的命名函数。我只是个人更喜欢把我的钩子和逻辑放在一起,而不是追逐名字。再一次,完全是个人喜好。

    如果您仍然收到内部错误,请确保同时启用 WordPress 和 PHP 调试,以便您可以看到实际错误是什么,这可能是我的一个小错误。

    add_action(
        'init',
        static function () {
            add_feed(
                'quotes',
                static function () {
    
                    // Override the main query since the render functions wants to use the loop
                    global $wp_query;
                    $wp_query = new WP_Query(
                        [
                            'post_type' => 'quotes',
                            'post_count' => 10,
                            'tax_query' => [
                                [
                                    'taxonomy' => 'quote_category',
                                    'field' => 'name',
                                    'terms' => 'Dogs',
                                ],
                            ],
                        ]
                    );
    
    
                    get_template_part('rss', 'quotes');
                }
            );
        }
    );
    

    【讨论】:

    • 谢谢克里斯,当我插入您的查询时,我的 RSS 提要给了我一个内部服务器错误。我正在使用function customRSS(){add_feed('quotes', 'RSS_quotes');}function RSS_quotes(){get_template_part('rss', 'quotes');}
    • 我已经在上面更新了我的答案,希望可以,但是如果你仍然遇到错误,请确保打开 PHP 和 WordPress 调试以获取具体的错误消息和行号
    • 谢谢,克里斯。我将此称为答案,但我也发现我的原始函数的问题是我同时指定了 post_type 和 quote_category。这创造了一个“或”的情况,结果比我想要的要多。就我而言,我可以简单地消除 post_type 元素。
    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多