WP_Query 的两个查询附加第二个以分页结束

htm*_*vis 4 wordpress wordpress-theming

我想做的是为属性创建两个查询。人们将根据正常查询检索常规结果。第二个查询将检索与第一个查询密切相关的属性。我能够运行两个查询并检索所有结果,同时将 posts_per_page 设置为无限制且无分页。添加分页时的问题是循环运行并在每个页面上显示帖子。

该页面将有第一个循环中的 3 个,然后是第二个循环中的 3 个。

我试图将两个查询合并为一个并显示它们,但发生了相同的结果。3 和 3。

我想我需要以某种方式追加以确保第二个循环在第一个循环之后获得输出。有什么想法吗?

这是我的循环(由于长度我排除了 args)

<?php 
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query(); 

$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
?>      

<?php foreach($results->posts as $post) : ?>
  <?php setup_postdata( $post ); ?>
  <?php get_template_part( 'property-listing' ); ?>

<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)

alp*_*ego 5

因为parse_query依赖于post_count你必须添加两个 post_counts。在你的例子post_count中没有设置。如果您填充 post_count,它应该可以工作。只需在最后添加:

$results->post_count = $queryOne->post_count + $queryTwo->post_count;
Run Code Online (Sandbox Code Playgroud)

您的完整示例:

<?php 
  $queryOne = new WP_Query($args);
  $queryTwo = new WP_Query($args2);
  $results = new WP_Query(); 

  $results->posts = array_merge($queryOne->posts, $queryTwo->posts);
  $results->post_count = $queryOne->post_count + $queryTwo->post_count;

  foreach($results->posts as $post) : 
     setup_postdata( $post );
     get_template_part( 'property-listing' );

  endforeach;
?>
Run Code Online (Sandbox Code Playgroud)