I have a website that advertises companies categorised by activity and location. Sometimes a company advertises an activity in more than one location, and the client has added these listings in multiple times - once for each location.
Each activity has its own page, and lists all of the companies advertising that particular activity.
I want to be able to hide the duplicates - the companies that have been added in more than once because they're available in multiple locations.
This is the current query and output code:
<?php $activities = new WP_Query('cat=4&meta_key=activity&meta_value='.$activity.''); ?>
<?php if($activities->have_posts()) : ?><?php while($activities->have_posts()) : $activities->the_post(); ?>
<?php $thumbid = get_post_thumbnail_id($post->ID); ?>
<div class="listing">
<div class="left">
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=60&w=60&zc=1" alt="<?php the_title(); ?>" width="60" height="60" />
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
</div>
<div class="right">
<?php if (post_custom('address')) { ?><p><?php echo get_post_meta($post->ID, "address", $single = true); ?></p><?php } ?>
<?php if (post_custom('phone')) { ?><p><strong>t: </strong><?php echo get_post_meta($post->ID, "phone", $single = true); ?></p><?php } ?>
<?php if (post_custom('email')) { ?><p><strong>e: </strong><?php echo get_post_meta($post->ID, "email", $single = true); ?></p><?php } ?>
<?php if (post_custom('website')) { ?><p><a href="<?php echo get_post_meta($post->ID, "website", $single = true); ?>">Visit website</a></p><?php } ?>
</div>
<div class="cboth"></div>
</div>
<?php endwhile; endif; ?>
I'd like to exclude any posts that have the same name as a post that are already shown on the listing page - so each listing is unique on an activity page.
Konrad Karpieszuk answers:
hi :)
before this code which you submitted, on beginning add line:
<?php $titles = array(); ?>
and then after second line of your code (this with "if" statement) add those lines:
<?php
if (in_array($post->post_title, $titles)) { continue; }
$titles[] = $post->post_title;
?>
Oleg Butuzov answers:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->posts.'.post_title';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $activities = new WP_Query('cat=4&meta_key=activity&meta_value='.$activity.''); ?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
example updated for a proper version. tested. it works! =)
Buzu B answers:
An easy way to do it would be to keep track of all the titles. You can store them in an array and then check every post to see if the title is already in the array, if it is, you skip to the next post, otherwise, you display the post. That should work, and the code would look like this:
<?php $activities = new WP_Query('cat=4&meta_key=activity&meta_value='.$activity.'');
$posts_tracker = array();
?>
<?php if($activities->have_posts()) : ?><?php while($activities->have_posts()) : $activities->the_post(); ?>
<?php if(!in_array($post->post_title, $posts_tracker)){ ?>
<?php $posts_tracker[] = $post->post_title; ?>
<?php $thumbid = get_post_thumbnail_id($post->ID); ?>
<div class="listing">
<div class="left">
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=60&w=60&zc=1" alt="<?php the_title(); ?>" width="60" height="60" />
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
</div>
<div class="right">
<?php if (post_custom('address')) { ?><p><?php echo get_post_meta($post->ID, "address", $single = true); ?></p><?php } ?>
<?php if (post_custom('phone')) { ?><p>t: <?php echo get_post_meta($post->ID, "phone", $single = true); ?></p><?php } ?>
<?php if (post_custom('email')) { ?><p>e: <?php echo get_post_meta($post->ID, "email", $single = true); ?></p><?php } ?>
<?php if (post_custom('website')) { ?><p><a href="<?php echo get_post_meta($post->ID, "website", $single = true); ?>">Visit website</a></p><?php } ?>
</div>
<div class="cboth"></div>
</div>
<?php } ?>
<?php endwhile; endif; ?>
I hope that helps.