在一个WP_QUERY中合并两个参数并按日期排序

时间:2014-02-24 作者:victor

我正在尝试将2个WP\\U查询合并到一个新的WP\\U查询中,然后按日期降序排列此新合并查询中的所有帖子。我可以合并2个WP\\u查询并正常工作,但我无法对新查询排序

    $args1 = array(
             \'posts_per_page\' => \'10\',
             \'post_status\' => \'publish\',
             \'post_type\' => array(\'news\',\'partners\'),
             \'orderby\' => \'date\',
             \'order\' => \'DESC\'
             );
    $queried_post1 = new WP_Query($args1);

    $args2 = array(
             \'posts_per_page\' => \'10\',
             \'post_status\' => \'publish\',
             \'post_type\' => array(\'post\'),
             \'orderby\' => \'date\',
             \'order\' => \'DESC\',
             \'tax_query\' => array(array(
                            \'taxonomy\' => \'tax\',
                            \'field\' => \'id\',
                            \'terms\' => \'5\'
                            ))
             );
             $queried_post2 = new WP_Query($args2);

             $merged_queried_post = new WP_Query();
             $merged_queried_post->posts = array_merge($queried_post1->posts,$queried_post2->posts);
             $merged_queried_post->post_count = $queried_post1->post_count + $queried_post2->post_count;
在这一点上,我需要按日期在$merged\\u queryed\\u post中排序所有帖子。

谢谢你的帮助

2 个回复
最合适的回答,由SO网友:Shazzad 整理而成

更好的方法是使用三个查询。前两个查询检索post ID,第三个查询按ID查询post。

// first query
$first_ids = get_posts( array(
    \'fields\'         => \'ids\',
    \'posts_per_page\' => \'10\',
    \'post_status\'    => \'publish\',
    \'post_type\'      => array(\'news\',\'partners\'),
    \'orderby\'        => \'date\',
    \'order\'          => \'DESC\'
));

// second query
$second_ids = get_posts( array(
    \'fields\'         => \'ids\',
    \'posts_per_page\' => \'10\',
    \'post_status\'    => \'publish\',
    \'post_type\'      => array(\'post\'),
    \'orderby\'        => \'date\',
    \'order\'          => \'DESC\',
    \'tax_query\'      => array(array(
        \'taxonomy\'       => \'tax\',
        \'field\'          => \'term_id\',
        \'terms\'          => array(5)
    ))
));

// merging ids
$post_ids = array_merge( $first_ids, $second_ids);

// the main query
$query = new WP_Query(array(
    \'post_type\' => \'any\',
    \'post__in\'  => $post_ids, 
    \'orderby\'   => \'date\', 
    \'order\'     => \'DESC\'
));

if( $query->have_posts() ):
    // here you go
endif;

SO网友:Davor Popovic

只需一个通知。。。此查询也适用于我。。但是如果你用粘帖。。不要忘了添加“ignore\\u sticky\\u posts”=>1,也不要忘了添加while-endwhile。。。这是我的查询中的问题。。。因此,“主查询”将是

// the main query
$query = new WP_Query(array(
    \'post_type\' => \'any\',
    \'post__in\'  => $post_ids, 
    \'orderby\'   => \'date\', 
    \'order\'     => \'DESC\',
    \'ignore_sticky_posts\' => 1
));

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
echo the_title().\'<br>\';
endwhile; endif;

结束

相关推荐

orderby:date not working

我一直在寻找答案,似乎有很多相反的问题的答案。我正在尝试使用以下代码按发布日期排序,而不是按修改日期排序:query_posts($query_string . \'&orderby=date&order=DESC&posts_per_page=-1\'); 但它仍然是按修改日期排序而不是发布日期,有人能帮忙吗?提前感谢!