我会在顶部添加一个数组,以便在您遍历所有交易时存储公司的ID。
每次通过循环时,都会检查阵列中的当前公司ID。如果没有,则可以安全地显示该交易。然后将ID添加到数组中,以便下次该公司出现时,您可以跳过该公司的任何其他交易。
下面是您的新代码的外观。(我认为,用适当的变量替换“当前公司ID”,以便通过ID跟踪您的公司。)$term->ID
, 但我承认,我只读了你的代码,对它的主要功能有了一个大致的了解。)
<?php
$num_cats_to_show = 50;
$count = 0;
/*----NEW STUFF----*/
$company_id_array = array();//NEW VARIABLE
$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 ++;
/*----NEW STUFF----*/
if ($count <= $num_cats_to_show && !(in_array("the current company ID", $company_id_array))) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
/*----NEW STUFF----*/
$company_id_array[] = "the current company ID";//ADD THE ID TO THE ARRAY (YOU HAVE ALREADY CHECKED THAT IT WASN\'T THERE YET)
<?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().
?>