0

I have a loop with max. 50 posts for a custom post type, called deals. Each of these has a custom field assigned, named "company", set as an ID, assigned to another custom post type named "companies". These are set using the Relationship field from ACF.

I want the loop on the homepage show all deals, however, never show more than one deal from the same company. Now, I could make 50 custom loops for each company, however I do want the latest deal shown on top of the page.

I'm thinking of having a foreach set with the custom post type, however I am not sure how to do this.

This the loop I currently have:

<?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().
?>

This works, in theory, however the posts get sorted by their company, and not by deal.

0

I would add an array at the top to store the company's ID as you loop through all the deals.

Every time you go through the loop you check the array for the current company ID. If it isn't there then you can safely show that deal. Then add the ID to the array so that next time that company comes around you will be able to skip any other deals for that company.

Here is what your new code might look like. (Replace "the current company ID" with the proper variable to keep track of your companies by ID, I think $term->ID, but I admittedly only read your code enough to get a fair idea of the main function of it)

<?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().
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.