4

我想做的是为属性创建两个查询。将根据正常查询检索常规结果。第二个查询将检索与第一个查询密切相关的属性。我能够运行这两个查询并检索所有结果,并将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; ?>
4

1 回答 1

5

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

$results->post_count = $queryOne->post_count + $queryTwo->post_count;

您的完整示例:

<?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;
?>
于 2013-11-28T21:34:51.190 回答