3 added 89 characters in body
source | link

I have a loop with max. 50 posts for a custom post type, called deals. Each of these has a custom post typefield assigned, named "company", set as an ID, assigned to another custom post type named "company""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().
?>

It almostThis works, but somehow it still displaysin theory, however the sameposts get sorted by their company, and not by deal twice sometimes.

I have a loop with max. 50 posts for a custom post type, called deals. Each of these has a custom post type assigned, named "company", set as an ID, assigned to another custom post type named "company". 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),
                            '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().
?>

It almost works, but somehow it still displays the same deal twice sometimes.

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.

2 added 579 characters in body
source | link
<?php
$num_cats_to_show //= WP_Query50;
$count arguments= 0;
$args
$posts = arraynew WP_Query(
 array('post_type' => 'deals','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' => '6'($term->ID),
                                'compare' => 'LIKE'
                            )
                    ),
    )  'posts_per_page' => 1,
      );
 
// The Query  $my_query = null;
$query    $my_query = new WP_Query( $args $args2);
 
// The Loop
if  if( $query$my_query->have_posts() ) {
while ( $query->have_posts    $count ++;
      if ()$count <= $num_cats_to_show) {
    $query    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a      <?php echo $term->ID ?><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>a></p>
         <?php 
 }       endwhile;
} else {    }
// no posts found }
  }
}
wp_reset_query();  // Restore originalglobal Postpost Data
data wp_reset_postdatastomped by the_post(); .
?>

It almost works, but somehow it still displays the same deal twice sometimes.

<?php // WP_Query arguments
$args = array (
 'post_type' => 'deals',
                        'meta_query' => array(
                            array(
                                'key' => 'company',
                                 'value' => '6',
                                'compare' => 'LIKE'
                            )
                        )
);
 
// The Query
$query = new WP_Query( $args );
 
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php }
} else {
// no posts found
}

// Restore original Post Data
 wp_reset_postdata(); ?>
<?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),
                            '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().
?>

It almost works, but somehow it still displays the same deal twice sometimes.

1
source | link

Wordpress loop, show only one post per custom field

I have a loop with max. 50 posts for a custom post type, called deals. Each of these has a custom post type assigned, named "company", set as an ID, assigned to another custom post type named "company". 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 // WP_Query arguments
$args = array (
'post_type' => 'deals',
                        'meta_query' => array(
                            array(
                                'key' => 'company',
                                'value' => '6',
                                'compare' => 'LIKE'
                            )
                        )
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php }
} else {
// no posts found
}

// Restore original Post Data
 wp_reset_postdata(); ?>