Duplicate posts being displayed when querying Custom Fields

I have created a small segment within WordPress that records match information for League of Legends. Using up to 30 different custom fields I can store information of 30 champions picked from 3 games (10 different champions are picked each game)

I want to query all games the champion ‘Shen’ has played in but the issue have come across is, if ‘shen’ was Picked in two of the three games played, that individual match is displayed twice in the loop. Is it possible to prevent any duplicates if a custom field has duplication information?

Read More

Code I am using currently:

<?php

$querystr = "
SELECT $wpdb->posts.* 
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
AND $wpdb->postmeta.meta_value = 'Shen' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'matches'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";

$pageposts = $wpdb->get_results($querystr, OBJECT);

?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>

<?php setup_postdata($post); ?>

<div class="matchesh">
<div class="vs">vs</div>
<div class="vdetails"><a href="<?php the_permalink(); ?>">view match information</a></div>
<div class="vevent"><?php echo get_post_meta($post->ID, 'dbt_tournamentname', true) ?></div>
<div class="teamleft"><img src="<?php echo get_post_meta($post->ID, 'dbt_team1logo', true) ?>" /></div>
<div class="teamright"><img src="<?php echo get_post_meta($post->ID, 'dbt_team2logo', true) ?>" /></div>

<div class="clearfix"></div>

</div>
<?php endforeach; ?>

<?php else : ?>
<h2 class="center">No matches found featuring 'Shen'.</h2>
<p class="center">Sorry, no team composition featured the selected champion.</p>

<?php endif; ?>

Any help will be greatly appreciated!
Cheers
Dan

Related posts

Leave a Reply

2 comments

  1. As @helenhousandi said, why not use the WP_Query, try this:

    <?php 
    // the args for the WP_Query 
    // See more @http://codex.wordpress.org/Class_Reference/WP_Query
    $args = array(
        'post_type'  => 'matches',
        'meta_value' => 'Shen',
        'order'      => 'DESC'
    );
    ?>
    
    <?php $the_query = new WP_Query( $args ); ?>
    
        <?php if( $the_query->the_post() ) : ?>
    
            <?php while ( $the_query->have_posts() ); ?>
    
    
                <?php $tournament = get_post_meta( $post->ID, 'dbt_tournamentname', true ); ?>
                <?php $team_logo_1 = get_post_meta( $post->ID, 'dbt_team1logo', true ); ?>
                <?php $team_logo_2 = get_post_meta( $post->ID, 'dbt_team2logo', true ); ?>
    
                <div class="matchesh">
                    <div class="vs"><?php _e('vs','mytheme'); ?></div>
                    <div class="vdetails"><a href="<?php the_permalink(); ?>"><?php _e('view match information','mytheme'); ?></a></div>
                    <div class="vevent"><?php echo $tournament; ?></div>
                    <div class="teamleft"><img src="<?php echo $team_logo_1; ?>" /></div>
                    <div class="teamright"><img src="<?php echo $team_logo_1; ?>" /></div>
                    <div class="clearfix"></div>
                </div>
    
    
            <?php endwhile; ?>
    
            <?php else : ?>
    
            <h2 class="center"><?php _e('No matches found featuring "Shen".','mytheme'); ?></h2>
            <p class="center"><?php _e('Sorry, no team composition featured the selected champion.','mytheme'); ?></p>
    
        <?php endif; ?>
    
    <?php wp_reset_postdata(); ?>