0

I think I'm missing something really simple but I'm at my wits end and hoping to be pointed in the right direction. A little background.

I'm working on a site for a creative agency. I'm using taxonomy to associate people and projects that they worked on.

Within my loop querying the post type 'people' then within that the loop I'm querying the post type 'projects' and within the projects query I'm passing an argument that querys the 'project_assocation' taxonomy. The loop as it is works fine when each creative has their own page but we're moving to a one page layout and this is where I'm running into an issue.

There are creatives who may have participated in one anothers project. So creative one may be associated with project one and creative two may be associated with project one as well. In which I need to display that.

What's happening is when I'm running the loop (in this instance it's running three times - to list three creatives) It's duplicating the projects. So creative one will have project one but creative two will have project one show twice and creative three will have project one shown three times.

What's happening is obvious, I'm just not entirely sure what to do about it.

Any help would be much appreciated. Code is below:

<!-- 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 -->

0

Never mind, I got it.

For anyone who may run into a similar issue. See below.

Main Loop:

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        unset($do_not_duplicate);
        $the_query->the_post();
    }
}

Secondary Loop:

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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