将每种帖子类型的查询与不同参数组合在一起


11

我正在网站上建立一个部分,将两种不同的帖子类型合并为一个循环,然后随机显示它们。问题是,我很难找到一种方法来限制每种类型的帖子数量。

这是我尝试过的:

  • 一个具有多种帖子类型的查询可以通过一个数组来实现:

    $args = array( 'post_type' => array( 'photos', 'quotes' ), ...

    ...但不能限于每种类型的特定帖子数。

  • 在运行WP_Query之前合并两个查询参数数组:

    $photos = array( 'post_type' => 'photos', 'posts_per_page' => 15, 'orderby' => 'rand' );
    $quotes = array( 'post_type' => 'quotes', 'posts_per_page' => 5, 'orderby' => 'rand' );
    
    $args = $photos + $quotes;
    // Also tried array_merge( $photos, $quotes );

    没有运气。发生的是后者变量$quotes被覆盖$photos并且仅显示引号。

  • 通过类型转换将两个WP_Query对象合并在一起:

    $photos_query = new WP_Query( $photos );
    $quotes_query = new WP_Query( $quotes );
    $result = (object)array_merge( (array)$photos_query, (array)$quotes_query );

... 等等。

我可能可以直接对数据库使用SQL查询,但是我需要能够将这两个单独的帖子类型组合为一个循环,并随机排列,并且每种类型限制为一定数量的帖子。

谢谢你的帮助!

Answers:


16

一种方法是自定义使用posts_clauses或其他此类过滤器执行的SQL查询。要找到它们,请posts_clauses在“ wp-includes / query.php”中搜索并查看此行之前的一系列过滤器。这些都可以自定义查询的任何部分

您可以做的另一件事是在对象中手动合并查询的帖子

$photos_query = new WP_Query( $photos );
$quotes_query = new WP_Query( $quotes );
$result = new WP_Query();

// start putting the contents in the new object
$result->posts = array_merge( $photos_query->posts, $quotes_query->posts );

// here you might wanna apply some sort of sorting on $result->posts

// we also need to set post count correctly so as to enable the looping
$result->post_count = count( $result->posts );

您的第二个解决方案(没有SQL)成功了!现在,在进入循环之前,我可以完全控制最终查询的内容。谢谢你的帮助!
安迪·梅斯金

1
第一个比较困难,但是效率更高(第二个仍然有2个数据库查询)。我会说这
取决于

对完成第一个解决方案的方式会非常感兴趣!所需的过滤器等。这是否UNION在sql中为每个post_type 调用了某种?
所罗门·克洛森

@SolomonClosson这个过滤器可以提供帮助-codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses
Mridul Aggarwal

7

@mridual aggarwal您的回答非常好,但不幸的是,它不是真正地结合2,wp_query它仅显示了两个排列的帖子,我的意思是第一个的5个帖子,第二个的5个帖子,但没有全部排序,所以我有这个解决方案,至少可以实现我自己的目标

<?php
$term = get_term_by( 'slug', get_query_var( 'tag' ), "post_tag" );
$tagslug = $term->slug;
$post_types = get_post_types('','names');
?>
<?php
//first query
$blogposts = get_posts(array(
    'tag' => $tagslug, //first taxonomy
    'post_type' => $post_types,
    'post_status' => 'publish',
    ));
//second query
$authorposts = get_posts(array(
    'bookauthor' => $tagslug, //second taxonomy
    'post_type' => $post_types,
    'post_status' => 'publish',
    ));
$mergedposts = array_merge( $blogposts, $authorposts ); //combine queries

$postids = array();
foreach( $mergedposts as $item ) {
$postids[]=$item->ID; //create a new query only of the post ids
}
$uniqueposts = array_unique($postids); //remove duplicate post ids

$posts = get_posts(array(
        //new query of only the unique post ids on the merged queries from above
    'post__in' => $uniqueposts,  
    'post_type' => $post_types,
    'post_status' => 'publish',
    ));
foreach( $posts as $post ) :
setup_postdata($post);
?>
// posts layout
<?php endforeach; ?>
<?php wp_reset_postdata();?>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.