使用嵌套WP_QUERY时重复

时间:2016-01-10 作者:jasenmp

我想我错过了一些非常简单的事情,但我已经不知所措,希望被指向正确的方向。一点背景知识。

我在一家创意机构的网站上工作。我正在使用分类法将人们和他们所从事的项目联系起来。

在我的循环中查询post类型“people”,然后在该循环中查询post类型“projects”,并在projects查询中传递一个查询“project\\u association”分类的参数。当每个创意都有自己的页面时,循环就可以了,但我们正在转向单页面布局,这就是我遇到的问题。

有些创意人员可能参与了另一个项目。因此,创意一可能与项目一相关,创意二也可能与项目一相关。我需要在其中显示。

发生的事情是,当我运行循环时(在本例中,它运行了三次-列出三个创意),它复制了项目。所以创意一会有项目一,但创意二会有项目一的两次演出,创意三会有项目一的三次演出。

发生的事情是显而易见的,我只是不完全确定该怎么办。

任何帮助都将不胜感激。代码如下:

<!-- container -->
<div class="ui container">

    <h1>Us</h1>

    <div class="ui three column grid">

        <?php

        $args = array(

            \'post_type\' => \'people\',

            \'posts_per_page\' => 3,

            \'post___in\' => array(28,57,71)

        );

        $the_query = new WP_Query( $args );

        if ( $the_query->have_posts() ) :

            while ( $the_query->have_posts() ) :

                $the_query->the_post();

                // Title

                $title = get_the_title();

                $excerpt = get_the_excerpt();

                $content = get_the_content();

                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full-size\' );

                $title_id = ( str_replace( \' \', \'-\', strtolower( $title ) ) );

                echo \'<div class="column">\';

                echo \'<figure class="effect-zoe">\';

                echo \'<img src="\'.$image[0].\'" class="ui fluid image">\';


                echo \'<figcaption>\';


                echo \'<h2>\'.$title.\'</h2>\';

                $social_media_links = array(

                    \'facebook\',

                    \'twitter\',

                    \'linkedin\',

                    \'pinterest\',

                    \'youtube\',

                    \'instagram\',

                    \'vimeo\',

                    \'tumblr\'

                );


                if( $social_media_links ){

                    echo \'<p class="icon-links clear-children">\';

                    foreach( $social_media_links as $social_media_link ){

                        $field = get_field_object( $social_media_link );

                        switch( $social_media_link ) {

                            case \'facebook\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-facebook"></i></a>\';

                                break;

                            case \'twitter\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-twitter"></i></a>\';

                                break;

                            case \'linkedin\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-linkedin"></i></a>\';

                                break;

                            case \'pinterest\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-pinterest"></i></a>\';

                                break;

                            case \'youtube\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-youtube"></i></a>\';

                                break;

                            case \'instagram\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-instagram"></i></a>\';

                                break;

                            case \'vimeo\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-vimeo"></i></a>\';

                                break;

                            case \'tumblr\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-tumblr"></i></a>\';

                                break;

                        }

                    }

                    echo \'</p>\';

                }

                echo \'<p class="description">\'.$excerpt.\'<br/><a href="#\'.$title_id.\'"class="cd-btn">View Profile</a></p>\';



                echo \'</figcaption>\';


                echo \'</figure>\';


                echo \'<div id="\'.$title_id.\'" class="cd-panel from-left">\';


                echo \'<header class="cd-panel-header"><h1>\'.$title.\'</h1><a href="#0" class="cd-panel-close">Close</a></header>\';


                echo \'<div class="cd-panel-container">\';


                echo \'<div class="cd-panel-content">\';


                echo \'<div class="cd-panel-content-image">\';


                // echo \'<img src="\'.$image[0].\'">\';


                echo \'<img src="http://placehold.it/960x400" class="ui fluid image">\';


                echo \'</div>\';

                // .cd-panel-content-image


                echo \'<div class="cd-content-inner">\';


                echo \'<h1>\'.$title.\'</h1>\';

                echo \'<div class="meta">\';

                $terms = get_the_terms($post->ID, \'profession_type\');

                if ($terms) {

                    $term_array = array();

                    foreach($terms as $termname) {

                        $term_result = $termname->slug;

                        $term_array[] = $term_result;

                    }

                    $terms_list = implode(\', \', $term_array);

                    echo \'<strong>Profession(s):</strong> \'.str_replace( \'-\', \' \', $terms_list );

                }

                echo \'</div>\';


                echo wpautop($content);

                // Social Links

                if( $social_media_links ){

                    echo \'<p class="icon-links clear-children">\';

                    foreach( $social_media_links as $social_media_link ){

                        $field = get_field_object( $social_media_link );

                        switch( $social_media_link ) {

                            case \'facebook\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-facebook"></i></a>\';

                                break;

                            case \'twitter\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-twitter"></i></a>\';

                                break;

                            case \'linkedin\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-linkedin"></i></a>\';

                                break;

                            case \'pinterest\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-pinterest"></i></a>\';

                                break;

                            case \'youtube\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-youtube"></i></a>\';

                                break;

                            case \'instagram\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-instagram"></i></a>\';

                                break;

                            case \'vimeo\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-vimeo"></i></a>\';

                                break;

                            case \'tumblr\':

                                echo \'<a target="_blank" href="\'.$field[\'value\'].\'"><i class="fa fa-tumblr"></i></a>\';

                                break;

                        }

                    }

                    echo \'</p>\';

                }

                // Related Projects

                $project = get_terms( \'project_association\' );

                if ( $project ) {

                    echo \'<h4>My Projects</h4>\';

                    foreach( $project as $projects ) {

                        $project_list_array[] = $projects->name;

                    }

                    $people_title = get_the_title();

                    echo \'<div class="ui four column grid">\';

                    foreach( $project_list_array as $name ) {

                        if ( $name == $people_title ) {

                            $args = array( \'post_type\' => \'projects\', \'posts_per_page\' => 1,

                                \'tax_query\' => array(
                                    array(

                                        \'taxonomy\' => \'project_association\',

                                        \'field\'    => \'name\',

                                        \'terms\'    => $name,

                                    ),
                                )
                            );

                            $projects_query = new WP_Query( $args );

                            if ( $projects_query->have_posts() ) :

                                while ( $projects_query->have_posts() ) :

                                    $projects_query->the_post();

                                    $project_img_id = get_post_thumbnail_id($post->ID);

                                    $project_image = wp_get_attachment_image_src($project_img_id, $optional_size);

                                    $project_title = get_the_title();

                                    $project_id = ( str_replace( \' \', \'-\', strtolower( $title ) ) );

                                    $project_excerpt = get_the_excerpt();

                                    $alt_text = get_post_meta($img_id , \'_wp_attachment_image_alt\', true);


                                    echo \'<div class="column">\';


                                    echo \'<figure class="effect-zoe">\';


                                    echo \'<img src="\'.$project_image[0].\'" class="ui fluid image">\';


                                    echo \'<figcaption>\';


                                    echo \'<h5>\'.$project_title.\'</h5>\';


                                    echo \'<p class="description">\'.$project_excerpt.\'<br/><a href="#\'.$project_id.\'"class="cd-btn">View Profile</a></p>\';


                                    echo \'</figcaption>\';


                                    echo \'</figure>\';


                                    echo \'</div>\';

                                    // .column

                                endwhile;

                            endif;

                        }

                    }

                    echo \'</div>\';

                    // .grid

                }

                echo \'</div>\';


                // .cd-panel-content-inner


                echo \'</div>\';

                // .cd-panel-content


                echo \'</div>\';

                // .cd-panel-container


                echo \'</div>\';

                // .overlay


                echo \'</div>\';

                // .column

            endwhile;

        endif;

        ?>

    </div>
    <!--/.ui.grid -->

</div>
<!-- /.container -->

1 个回复
最合适的回答,由SO网友:jasenmp 整理而成

没关系,我知道了。

对于任何可能遇到类似问题的人。见下文。

主回路:

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        unset($do_not_duplicate);
        $the_query->the_post();
    }
}
二次回路:

if ( $projects_query->have_posts() ) {
    while ( $projects_query->have_posts() ) {
        $do_not_duplicate[] = get_the_ID();
        if( in_array( get_the_ID(), $do_not_duplicate) ) 
            continue;
    }
}