admin管理员组文章数量:1306739
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.
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.
Share Improve this question edited Oct 22, 2015 at 10:23 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Oct 22, 2015 at 7:53 Janith ChinthanaJanith Chinthana 4082 gold badges8 silver badges26 bronze badges 03 Answers
Reset to default 5Here 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
}
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.
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;
本文标签: wp queryAdd few specific post ids to wpquery
版权声明:本文标题:wp query - Add few specific post ids to wp_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741823516a2399509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论