查询自定义字段时显示重复的帖子

时间:2012-06-05 作者:Dan Whiteside

我在WordPress中创建了一个小片段,用于记录传奇联盟的比赛信息。使用最多30个不同的自定义字段,我可以存储从3场比赛中挑选的30名冠军的信息(每场比赛挑选10名不同的冠军)

我想查询冠军“沈”参加过的所有比赛,但遇到的问题是,如果“沈”在三场比赛中的两场比赛中被选中,那么这场个人比赛将在循环中显示两次。如果自定义字段具有重复信息,是否可以防止任何重复?

我当前使用的代码:

<?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; ?>
任何帮助都将不胜感激!切尔斯丹

2 个回复
SO网友:Tommixoft

只需添加到您的查询SELECT DISTINCT $wpdb->posts. 而不是SELECT $wpdb->posts.

SO网友:Pontus Abrahamsson

正如@helenhousandi所说,为什么不使用WP\\u查询,请尝试以下操作:

<?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(); ?>

结束

相关推荐

Update Loop with Form

我有一个照片库,我正在开发使用WP作为内容管理手段。我非常依赖一些jQuery插件来管理UI样式和操作(通过排序)。我的问题是有太多该死的帖子!737,每个都有一个缩略图,显示在页面上。这不可避免地会使任何浏览器陷入困境。尤其是排序插件在对图像进行排序时会“克隆”图像。帖子使用Wp\\u查询脚本构建;<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page =