我有一个循环,最多50个自定义帖子类型的帖子,称为deals。其中每一个都分配了一个名为“company”的自定义字段,设置为ID,并分配给另一个名为“companys”的自定义帖子类型。这些设置使用Relationship field 来自ACF。
我希望主页上的循环显示所有交易,但是,不要显示同一公司的多个交易。现在,我可以为每家公司制作50个自定义循环,但我确实希望最新的交易显示在页面顶部。
我正在考虑使用自定义post类型的foreach集,但是我不知道如何做到这一点。
这是我当前拥有的循环:
<?php
$num_cats_to_show = 50;
$count = 0;
$posts = new WP_Query(array(\'post_type\' => \'companies\'));
$terms = $posts->get_posts();
if ($terms) {
foreach( $terms as $term ) {
$args2=array(
\'post_type\' => \'deals\',
\'meta_query\' => array(
array(
\'key\' => \'company\', // name of custom field
\'value\' => \'"\'. ($term->ID) .\'"\', // matches exaclty "123", not just 123. This prevents a match for "1234"
\'compare\' => \'LIKE\'
)
),
\'posts_per_page\' => 1,
);
$my_query = null;
$my_query = new WP_Query($args2);
if( $my_query->have_posts() ) {
$count ++;
if ($count <= $num_cats_to_show) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo $term->ID ?><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
从理论上讲,这是可行的,但是这些帖子是按公司排序的,而不是按交易排序的。