向wp_Query添加几个特定的帖子ID

时间:2015-10-22 作者:Janith Chinthana

我有以下参数来获取最近的帖子,

$args = array(
    \'date_query\' => array( array( \'after\' => \'1 week ago\' ) ),  
    \'posts_per_page\' => 15,
    \'ignore_sticky_posts\' => 1,
    \'meta_key\' => \'post_views_count\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',
    \'cat\' => \'-907,-908,-909\'
);
现在,我需要为同一个查询包含几个特定的ID。所以我试着跟着,但没用

$highlights = array(\'8308\', \'8315\');

$args = array(
    \'date_query\' => array( array( \'after\' => \'1 week ago\' ) ),  
    \'posts_per_page\' => 15,
    \'ignore_sticky_posts\' => 1,
    \'meta_key\' => \'post_views_count\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',
    \'cat\' => \'-907,-908,-909\',
    \'post__in\' => $highlights
);
现在它只给了我这两个帖子。

3 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这里是另一种方法,特别是当您需要使用查询对象时。合并查询的问题是您失去了查询对象的正确性。

我的想法是运行两个独立的查询,一个非常精简的查询从date_query 查询并合并post ID的两个数组,然后将这些ID传递给适当的WP_Query 查询

注意:此代码至少需要PHP 5.4)

$highlights = [8308, 8315];

$args = [
    \'date_query\'          => [
        [
            \'after\'       => \'1 week ago\' 
        ]
    ],
    \'posts_per_page\'      => 13,
    \'meta_key\'            => \'post_views_count\',
    \'orderby\'             => \'meta_value_num\',
    \'order\'               => \'DESC\',
    \'cat\'                 => \'-907,-908,-909\',
    \'post__not_in\'        => $highlights,
    \'fields\'              => \'ids\' // ONLY GET POST ID\'s, VERY LEAN QUERY
];
$q = get_posts( $args );

$merged_ids = array_merge( $highlights, $q );
请注意,高亮显示在查询的前面,来自$q的帖子是按日期排序的。如果需要保留此订单,只需添加\'orderby\' => \'post__in\', 以下查询的参数

if ( $merged_ids ) {
    $args_final = [
        \'post__in\'            => $merged_ids,
        \'posts_per_page\'      => -1,
        \'orderby\'             => \'post__in\',
        \'ignore_sticky_posts\' => 1
    ];
    $query_final = new WP_Query( $args_final );

    // Run your loop
}

SO网友:Janith Chinthana

我不确定这是不是最好的方法,但我成功地使用了以下代码。

$highlights = array(\'8308\', \'8315\');

$args1 = array(
    \'post__in\' => $highlights
);

$args2 = array(
    \'date_query\' => array( array( \'after\' => \'1 week ago\' ) ),  
    \'posts_per_page\' => 13,
    \'ignore_sticky_posts\' => 1,
    \'meta_key\' => \'post_views_count\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',
    \'cat\' => \'-907,-908,-909\',
    \'post__not_in\' => $highlights
);

$query1 = new WP_Query($args1);
$query2 = new WP_Query($args2);

$products = new WP_Query();
$products->posts = array_merge( $query1->posts, $query2->posts );
$products->post_count = $query1->post_count + $query2->post_count;
$products 是最终的post数组。

SO网友:samjco

事实上,我发现这个解决方案非常通用:

$highlights = array(\'8308\', \'8315\');
 
//Optionally, to get these IDS (or others, maybe of the different posttype, or whatever) you can use a simple args with get_posts function and return just the ids using the arg fields => ids


$args2 = array(
    \'date_query\' => array( array( \'after\' => \'1 week ago\' ) ),  
    \'posts_per_page\' => 13, //<-- Make sure to enter the max (total) amount for both
    \'ignore_sticky_posts\' => 1,
    \'meta_key\' => \'post_views_count\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',  //<-- this is disabled, because we will enable later
    \'cat\' => \'-907,-908,-909\',
    \'fields\' => \'ids\' // Returns nothing but IDs
);

  $args2 = get_posts($args2); //returns IDs 

  $merged_args = array_merge( $args2, $highlights); //Let\'s merge the IDS..
  $merged_args = array_unique( $merged_args ); // get only uniques... 


  $firstQuery= new WP_Query( array(
       \'post__in\' => $merged_args, 
       \'posts_per_page\' => 2, // Only return 2 post out of the MAX of 13
       \'ignore_sticky_posts\' => true, 
       \'orderby\' => \'date\' 
        )  
     );


if ($firstQuery->have_posts()) : // The Loop
$firstQuery_count= 0;

 while ($firstQuery->have_posts()): $firstQuery->the_post();
 $firstQuery_count++;

 //Do something here with firstQuery....

 endwhile;
  wp_reset_query();

endif;

  $secondQuery= new WP_Query( array(
         \'post__in\' => $merged_args, 
         \'offset\' => 2, // do not forget to add an offset of the 2 from the firstQuery
         \'posts_per_page\' => 11, // Only return 11 post out of the MAX of 13
         \'ignore_sticky_posts\' => true, 
         \'orderby\' => \'date\' 
        )  
     );


if ($secondQuery->have_posts()) : // The Loop
$secondQuery_count= 0;

 while ($secondQuery->have_posts()): $secondQuery->the_post();
 $secondQuery_count++;

   //Do something here with secondQuery....

 endwhile;
  wp_reset_query();

endif;