2

I have following args to get recent posts,

$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'
);

Now I need include few specific Ids to same query. So I tried follows, but it doesn't work

$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
);

Now it just giving me those two posts.

0

3 Answers 3

5

Here is another way of doing it, specially if you need to work with the query object. The issue with merging queries is that you loose the correctness of the query object.

My idea here is to run two separate queries, one very lean one to get post ID's from the date_query query and then merging the two arrays of post ID's and then pass those ID's to proper WP_Query query.

(NOTE: This code requires at least 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 );

Note now, the hightlights are in front of the query, and the posts from $q is ordered by date. If you need to retain this order, simply add 'orderby' => 'post__in', to the arguments of the query below

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
}
2

I am not sure, is this the best way, but I succeed with following code.

$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 is the final post array.

2
  • That is about the best you can do if you are not going to make use of sticky posts Oct 22, 2015 at 8:35
  • thanks @PieterGoosen , since I can't use sticky posts I will go with this option Oct 22, 2015 at 8:44
0

Actually, I've found this solution to be very versatile:

$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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.