Property Status Duplicating Properties on WordPress Site

I am working with this site: http://f9properties.com/ and on the home page it has an area for “Featured Properties” and it has all these different attributes that define the properties including “Property Types”, “Property Cities” and “Property Status”. In each property, within the WordPress Dashboard, there is a way to check that you want the property to be featured and appear on the home page.

The problem is this, if the property has more than one type of status, in this case like “For Sale” and “For Lease”, the property shows up twice in the Featured Properties area. From what I can tell, the code for the Featured Area is this:

Read More
function Featured_Properties_Widget(){
    $widget_ops = array( 'classname' => 'Featured_Properties_Widget', 'description' => __('Displays Random or Recent Featured Properties','framework') );
    $this->WP_Widget( 'Featured_Properties_Widget', __('RealHomes - Featured Properties','framework'), $widget_ops );
}


function widget($args, $instance) { 

    extract($args);

    $title = apply_filters('widget_title', $instance['title']);     

    if ( empty($title) ) $title = false;    

    $count = intval( $instance['count']);           
    $sort_by = $instance['sort_by'];    


    $featured_args = array(
                        'post_type' => 'property',
                        'posts_per_page' => $count,
                        'meta_query' => array(
                                            array(
                                                'key' => 'REAL_HOMES_featured',
                                                'value' => '1',
                                                'compare' => 'LIKE'
                                            )
                                        )
                        );

    //Order by
    if($sort_by == "random"):
        $featured_args['orderby']= "rand";
    else:
        $featured_args['orderby']= "date";
    endif;          

    $featured_query = new WP_Query($featured_args);

    echo $before_widget;

    if($title):
        echo $before_title;
        echo $title;
        echo $after_title;
    endif;

    if($featured_query->have_posts()):
        ?>
        <ul class="featured-properties">
            <?php
            while($featured_query->have_posts()):
                $featured_query->the_post();
                ?>
                <li>
                    <?php
                    if(has_post_thumbnail()){
                        ?>
                        <figure>
                            <a href="<?php the_permalink(); ?>">
                                <?php the_post_thumbnail('grid-view-image');?>
                            </a>
                        </figure>
                        <?php
                    }
                    ?>
                    <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                    <p><?php framework_excerpt(7); ?> <a href="<?php the_permalink(); ?>"><?php _e('Read More','framework'); ?></a></p>
                    <span class="price"><?php property_price(); ?></span>
                </li>
                <?php
            endwhile;
            ?>
        </ul>
        <?php
        wp_reset_query();
    else:
        ?>
        <ul class="featured-properties">
            <?php
            echo '<li>';
            _e('No Featured Property Found!', 'framework');
            echo '</li>';
            ?>
        </ul>
        <?php   
    endif;

    echo $after_widget;
}


function form($instance) 
{   
    $instance = wp_parse_args( (array) $instance, array( 'title' => 'Featured Properties', 'count' => 1 , 'sort_by' => 'random' ) );

    $title= esc_attr($instance['title']);   
    $count =  $instance['count'];   
    $sort_by = $instance['sort_by'];

        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'framework'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Properties', 'framework'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Sort By:', 'framework') ?></label>
            <select name="<?php echo $this->get_field_name('sort_by'); ?>" id="<?php echo $this->get_field_id('sort_by'); ?>" class="widefat">
                    <option value="recent"<?php selected( $sort_by, 'recent' ); ?>><?php _e('Most Recent', 'framework'); ?></option>
                    <option value="random"<?php selected( $sort_by, 'random' ); ?>><?php _e('Random', 'framework'); ?></option>
            </select>
        </p>
        <?php
}

function update($new_instance, $old_instance) 
{
    $instance = $old_instance;      

    $instance['title'] = strip_tags($new_instance['title']);
    $instance['count'] = $new_instance['count'];
    $instance['sort_by'] = $new_instance['sort_by'];

    return $instance;

}

}
?>

Any assistance would be greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. OK, following on from my comment I’ll explain. There is probably a better way of achieving this by changing the meta_query arguments to make it more efficient (ie not pull out duplicates in the first instance) but what the link in my comments is doing is setting up a new empty array outwith the while loop, and then adding values to it as it iterates through the loop.

    In that example in the link it’s ‘country’, I’ve changed it so it checks the post ID, which is a better option for you as it’s a unique identifier. Every time it loops it adds the post id to the array $added, at the start of the loop it uses the PHP function in_array to check the post ID against all the values already added to $added. If it finds that the post ID is already in the array continue is invoked and the rest of the code in the loop is skipped and it goes back to the next iteration. So you won’t get duplicates outputted to the page.

        $added = array();
    
        if($featured_query->have_posts()):
            ?>
            <ul class="featured-properties">
                <?php
                while($featured_query->have_posts()):
                    $featured_query->the_post();
    
                    // bail early if this post ID has already been added
                    if( in_array(get_the_ID(), $added) )
                    {
                        continue;
                    }
    
                    // add post id to the array
                    $added[] = get_the_ID();
                    ?>
                    <li>
                        <?php
                        if(has_post_thumbnail()){
                            ?>
                            <figure>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail('grid-view-image');?>
                                </a>
                            </figure>
                            <?php
                        }
                        ?>
                        <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                        <p><?php framework_excerpt(7); ?> <a href="<?php the_permalink(); ?>"><?php _e('Read More','framework'); ?></a></p>
                        <span class="price"><?php property_price(); ?></span>
                    </li>
                    <?php
                endwhile;
                ?>
            </ul>
            <?php
            wp_reset_query();
        else:
            ?>
            <ul class="featured-properties">
                <?php
                echo '<li>';
                _e('No Featured Property Found!', 'framework');
                echo '</li>';
                ?>
            </ul>
            <?php   
        endif;