0

I'm using this code to get a list of custom posts (with custom fields) from the network for display in a php widget. It's old, but it's working fine:

<?php switch_to_blog(4);  ?>
<?php $ca = new WP_Query(array('post_type' => 'conference_announcem', 'order' => 'DESC', 'posts_per_page' => '-1'));
if ($ca->have_posts()): while( $ca->have_posts()): $ca->the_post(); ?>
<p align="left" class="style2">
<a href="<?php echo apply_filters('site_permalink_rewrite', get_permalink()); ?> "><strong><?php the_title(); ?></strong></a>
<?php
  if(get_field('front_page_text')) {
          echo '<br><strong>' . get_field('front_page_text') . '</strong>';
  }  ?>

<?php endwhile; ?>
<?php else: ?>
            <p><strong><?php _e('There are no conference announcements.'); ?></strong></p>
<?php endif; ?>
<?php restore_current_blog(); ?>

<?php switch_to_blog(2);  ?>
<?php $ca = new WP_Query(array('post_type' => 'conference_announcem', 'order' => 'DESC', 'posts_per_page' => '-1'));
if ($ca->have_posts()): while( $ca->have_posts()): $ca->the_post();
?>
<p align="left" class="style2">
  <a href="<?php echo apply_filters('site_permalink_rewrite', get_permalink()); ?>"><strong><?php the_title(); ?></strong></a>
<?php
  if(get_field('front_page_text')) {
          echo '<br><strong>' . get_field('front_page_text') . '</strong>';
  }  ?>

<?php endwhile; ?>
<?php else: ?>
            <p><strong><?php _e('There are no conference announcements.'); ?></strong></p>
<?php endif; ?>
<?php restore_current_blog(); ?>

<?php switch_to_blog(3);  ?>
<?php $ca = new WP_Query(array('post_type' => 'conference_announcem', 'order' => 'DESC', 'posts_per_page' => '-1'));
if ($ca->have_posts()): while( $ca->have_posts()): $ca->the_post(); ?>
<p align="left" class="style2">
  <a href="<?php echo apply_filters('site_permalink_rewrite', get_permalink()); ?>"><strong><?php the_title(); ?></strong></a>
<?php
if(get_field('front_page_text')) {
          echo '<br><strong>' . get_field('front_page_text') . '</strong>';
  }  ?>

<?php endwhile; ?>
<?php else: ?>
<p><strong><?php _e('There are no conference announcements.'); ?></strong></p>
<?php endif; ?>
<?php restore_current_blog(); ?>

I switch from blog to blog picking up the list of posts for display. My problem is that some posts are published in more than one blog, so naturally I'm getting duplicates. What is the best way to tell this script "If it is a duplicate, don't display it"? I'm sure there are lots ways of doing this, but I was hoping someone here would have a good suggestion for something fairly quick and simple (without a plugin). Thanks for any help.

9
  • How are they cross-posted?
    – s_ha_dum
    Jun 12, 2015 at 17:08
  • It's intentional that some conferences will be announced on more than one blog. But then the main site lists them all (minus the dupes, somehow).
    – user27442
    Jun 12, 2015 at 17:34
  • That doesn't answer the question. How is the cross-posting done in the first place? Do you simply copy content to a new post in each blog?
    – s_ha_dum
    Jun 12, 2015 at 17:37
  • Yeah, they're just copied. And the duplicate posts also have duplicate permalinks. That is, they all link to the same place using the Page Links To plugin.
    – user27442
    Jun 12, 2015 at 18:07
  • I am trying to work out a way to identify duplicates, if that isn't clear. You have a lot going on. Do post titles match 100%? Post slugs?
    – s_ha_dum
    Jun 12, 2015 at 18:40

1 Answer 1

2

SOLUTION:

I added

$titles = array(); 

at the top (outside the loop).

Then, within the loop, I used

array_push( $titles, get_the_title( $ID ) ); 

to collect the titles.

Then I just checked the array for a matching title before displaying, like this:

if ( !in_array(get_the_title( $ID ), $titles ) )
    ...then do this

And it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.