【问题标题】:Multiple post IDs from array in WordpressWordPress中数组中的多个帖子ID
【发布时间】:2017-07-17 20:03:25
【问题描述】:

我想显示 3 个具体的帖子。

问题:我的帖子 ID 来自以前的数组。

结果:只显示第一个。

功能:

foreach($fav_author_list as $i => $item) {
  $insert = get_user_favorites($item);
  if (!is_array($insert[0])) {
    $result = array_merge($result, $insert);
  }
}
$algoid = implode(",", $result); 

$algoid 的结果(帖子 ID)= 865、866、877

我想显示这三个帖子。

$myarray = array($algoid);
$args = array(
   'post__in'      => $myarray,
);
// The Query
$the_query = new WP_Query( $args );

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    您不必为post__in 内爆您的$algoid。由于您使用的是implode,因此您实际上是在为您的查询传递一个带有字符串的数组:

    array('865, 866, 877'); // Items: 1
    

    但是,WP_Query 需要一个带有 id 的数组,而不是字符串:

    array(865, 866, 877); // Items: 3
    

    应该是这样的:

    // Use your function to generate the array with the IDs
    $algoid = array(865, 866, 877); 
    
    $args = array(
        'post__in' => $algoid
    );
    

    更多关于WP_Query的信息:https://codex.wordpress.org/Class_Reference/WP_Query

    post__in (array) - 使用帖子 ID。指定要检索的帖子。注意如果您使用置顶帖子,无论您是否愿意,它们都将被包含(前置!)在您检索的帖子中。要抑制这种行为,请使用 ignore_sticky_posts。

    【讨论】:

    • 工作!谢谢
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多