【问题标题】:include option for get_posts works but is not limiting the number of postsget_posts 的包含选项有效,但不限制帖子数量
【发布时间】:2018-12-08 04:04:24
【问题描述】:

我正在使用 get_posts 的 include 选项,并且 numberposts 并不限制我现在设置的帖子数量,即 1,它只是加载所有设置的包含 ID。不能限制包含帖子ID的数量吗?这是我的代码。

$image_list = get_posts(array(
            'numberposts' =>  1,
            'include'   => '559,117,129',
            'post_type' => 'ft_gallery',
 ));

【问题讨论】:

  • 能否请您在此处运行此代码并复制结果查询以确保您传递给函数的内容? $getpost_attr['post_type'] = 'ft_gallery'; $getpost_attr['include'] = $this->albums_gallery_list_of_ids($image_list_check); $getpost_attr['posts_per_page'] = esc_html( $post_count ); $getpost_attr['orderby'] = esc_html( $orderby ); $getpost_attr['order'] = esc_html( $order ); $getpost_attr['paged'] = esc_html( $paged ); print_r($getpost_attr); 抱歉格式化,评论不允许我换行。
  • 我添加了返回数组
  • 只需使用 wp_reset_postdata();在foreach之后
  • @MounerMostafa 我不认为这有什么不同。看起来像 wp_reset_postdata();适用于 WP_Query 但我使用的是 get_posts。如果您可以在答案中展示更详细的解决方法,那就太棒了。
  • @amedv,抱歉,这是您请求数组的返回值([post_type] => ft_gallery [include] => 559,117,129,496,797 [posts_per_page] => 3 [orderby] => menu_order [order] => asc [分页] => 1)

标签: php wordpress pagination


【解决方案1】:

所以解决方案是使用 post__in 并确保数字列表采用数组格式。感谢您所做的一切努力。

$image_list = get_posts(array(
        'numberposts' =>  1,
        'post__in'   => array([0] => 559 [1] => 117 [2] => 129 [3] => 496 [4] => 797),
        'post_type' => 'ft_gallery',
));

【讨论】:

    【解决方案2】:

    使用 'numberposts'=> 2,如文档中所述:https://developer.wordpress.org/reference/functions/get_posts/。这是posts_per_page 的“别名”。在我的本地主机上用简码测试,它可以工作

    【讨论】:

    • 如果您不使用 include 选项,但添加指定的 id 列表并且它的行为与预期不符,则它可以工作
    • “按预期”是什么意思。我希望它会返回“包含”中的所有帖子以及任何粘性帖子。
    • 我的意思是因为我将帖子数设置为 1,但所有 5 个 id 都被返回。一切都很好,虽然我刚刚找到了解决方案并发布了答案。
    【解决方案3】:

    https://codex.wordpress.org/Template_Tags/get_posts

    注意:如果不设置 offset 参数,posts_per_page 参数将不起作用。

    【讨论】:

    • 我认为我在这里发现的更大问题是它与使用 include 选项有关,如果我删除它,那么一切正常,但我只需要特定的帖子 ID,而不是全部ft_gallery 中的帖子。
    • 按类别或标签过滤它们。将所有要循环的帖子放在同一类别中,不要使用特定的帖子 ID 来过滤它们。
    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多