Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Post Duplication

I am in charge of managing this site F9 Properties which is built in WordPress. On the home page there is a featured properties section. I noticed that if you listed a property with two different "Status" such as "For Sale or For Lease, the property appeared twice in the carousel. Below is the code for listing the featured properties. I can see that it filters out the properties with the Status "Leased". Can anyone help me add a bit of code to list only one property per post regardless of how many different property status it has?

<?php
/* Featured Properties Query Arguments */
$featured_properties_args = array(
'post_type' => 'property',
'posts_per_page' => 100,
'meta_query' => array(
    array(
        'key' => 'REAL_HOMES_featured',
        'value' => 1,
        'compare' => '=',
        'type'  => 'NUMERIC'
    )
)
);

$featured_properties_query = new WP_Query( $featured_properties_args );

if ( $featured_properties_query->have_posts() ) :
?>
<section class="featured-properties-carousel clearfix">
    <?php
    $featured_prop_title = get_option('theme_featured_prop_title');
    $featured_prop_text = get_option('theme_featured_prop_text');

    if(!empty($featured_prop_title)){
        ?>
        <div class="narrative">
           <h3><?php echo $featured_prop_title; ?></h3>
            <?php
            if(!empty($featured_prop_text)){
                ?><p><?php echo $featured_prop_text; ?></p><?php
            }
            ?>

        </div>
        <?php
    }

    ?>

       <div class="carousel es-carousel-wrapper">
        <div class="es-carousel">
            <ul class="clearfix">
                <?php
                while ( $featured_properties_query->have_posts() ) :
                    $featured_properties_query->the_post();
                    ?>

                    <?php
                $status_terms = get_the_terms( $post->ID,"property-status" );
                if(!empty( $status_terms )){
                    foreach( $status_terms as $status_term ){

                       if($status_term->name=="Leased"){}else{

                           ?>
                           <li>
                        <figure>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php
                                the_post_thumbnail('property-thumb-image',array(
                                    'alt'   => get_the_title($post->ID),
                                    'title' => get_the_title($post->ID)
                                ));
                                ?>
                            </a>
                        </figure>
                        <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                        <p><?php framework_excerpt(8); ?> <a href="<?php the_permalink() ?>"> <?php _e('Know More','framework'); ?> </a> </p>
                        <span class="price"><?php property_price(); ?></span>

                    </li>
                           <?
                       }


                    }
                }
                ?>

                    <?php
                endwhile;
                wp_reset_query();
                ?>
            </ul>
        </div>
    </div>
like image 821
CreativEliza Avatar asked Dec 03 '14 22:12

CreativEliza


People also ask

How do I avoid duplicate post displays with multiple loops in WordPress?

Avoiding Duplicate Posts in Multiple WordPress Loops In the following sample code, we are displaying posts for two categories without avoiding duplicate posts. wp_reset_postdata();

What does duplicate post mean in WordPress?

1. Duplicate Post. One of the go-to options for WordPress page and post cloning is Duplicate Post. This popular plugin is easy to use, and clones everything from the content of the page or post to the associated comments. It also offers a prefix or suffix option, to differentiate your original post and the clone.


1 Answers

I might be misunderstanding your setup, but I wonder why you're looping over the terms.

I think you should instead consider excluding the leased term within the WP_Query() part (hopefully you can share it).

Then your carousel would be simplified to:

<div class="carousel es-carousel-wrapper">
    <div class="es-carousel">
        <ul class="clearfix">
        <?php while ( $featured_properties_query->have_posts() ) : $featured_properties_query->the_post(); ?>
            <li><!-- YOUR POST ITEM HERE --></li>
        <?php endwhile; ?>
        </ul>
    </div>
</div>
like image 53
birgire Avatar answered Nov 10 '22 00:11

birgire