Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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

share|improve this question
    
Not necessarily relevant to the issue at hand, but why aren't you using a WP_Query instance or modifying the main query via pre_get_posts? –  helenhousandi Jun 5 '12 at 13:26
    
I tried a few different examples but this is the only one I could see some results with –  Dan Whiteside Jun 5 '12 at 14:04
    
Managed to do some more research on different ways of querying custom field data and this solution doesn't duplicate the contents <?php $args=array( 'meta_value'=>'Ahri', 'post_status'=>'publish', 'post_type'=>'matches', 'orderby'=>'date', 'order'=>'DESC' ); $wp_query= null; $wp_query = new WP_Query(); $wp_query->query($args); ?> –  Dan Whiteside Jun 5 '12 at 14:25
add comment

2 Answers

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(); ?>
share|improve this answer
add comment

just add to your query SELECT DISTINCT $wpdb->posts. instead of SELECT $wpdb->posts.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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