显示ID数组中的帖子

时间:2012-04-11 作者:Chuck

到目前为止,查询的结果是打印数组、数组([0]=>309[1]=>10)、309和;10是我想显示的帖子的正确id,而点击“else”的结果是“找不到帖子”。

我有一个粘贴箱,里面有我代码的最新副本,http://pastebin.com/pZkQCf9y

以下是查询:

<?php

$ongoing_args = array(
  \'post_type\' => \'promotions\',
  \'meta_key\' => \'sp_ongoingPromotion\',
  \'meta_value\' => 1
);

$current_args = array(
  \'post_type\' => \'promotions\',
  \'meta_key\' => \'sp_endDate\',
  \'meta_value\' => date("Y/m/d"),
  \'meta_compare\' => \'>=\',
  \'orderby\' => \'meta_value\',
  \'order\' => \'ASC\'
);

// Get promotions using the arguments outlined above.
$ongoing_promotions = get_posts( $ongoing_args );
$current_promotions = get_posts( $current_args );

// Merge arrays
$all_promotions = array_merge( $ongoing_promotions, $current_promotions );

// Get just the ID of promotions
$promotion_ids = wp_list_pluck( $all_promotions, \'ID\' );
print_r($promotion_ids);

// Do a new query with these IDs to get a properly-sorted list of promotions
$args = array(
  \'post__in\' => $promotion_ids,
  \'numberposts\' => 5,
  \'post_status\' => \'publish\'
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );

    $wp_query = new WP_Query($args);
    if($wp_query->have_posts()){
        while($wp_query->have_posts()){
            $wp_query->the_post();
?>
非常感谢您的帮助。

1 个回复
SO网友:Bainternet

如果您设置fields 参数到ids您将获得一个ID数组,而不是不需要的post数据,例如:

$ongoing_args = array(
  \'post_type\' => \'promotions\',
  \'meta_key\' => \'sp_ongoingPromotion\',
  \'meta_value\' => 1,
  \'fields\' => \'ids\'
);

$current_args = array(
  \'post_type\' => \'promotions\',
  \'meta_key\' => \'sp_endDate\',
  \'meta_value\' => date("Y/m/d"),
  \'meta_compare\' => \'>=\',
  \'orderby\' => \'meta_value\',
  \'order\' => \'ASC\',
  \'fields\' => \'ids\'
);

// Get promotions using the arguments outlined above.
$ongoing_promotions = get_posts( $ongoing_args );
$current_promotions = get_posts( $current_args );

// Merge arrays
$all_promotions = array_merge( $ongoing_promotions, $current_promotions );

$args = array(
  \'post__in\' => $promotion_ids,
  \'numberposts\' => 5,
  \'post_status\' => \'publish\'
);

$all_query = new WP_Query($args);
if($all_query->have_posts()){
    while($all_query->have_posts()){
        $all_query->the_post();
        //loop stuff here
    }
}

结束