Why are duplicate messages added when receiving data from the RETS server?

I am trying to add posts to wordpress from rets server using PHRETS. Unfortunately, duplicate posts are being added. I used WP request to validate an existing post using meta key and value. This code works well when I try to add 10 or 50 posts, but when I set the limit to 4000 it starts adding duplicate posts. I have run this code so many times and flushed the database many times. Here's some sample code:

$query = "(922=MIAMI), (246=A)";
$search = $rets->SearchQuery("Property", $class, $query, array("SystemName" => 1, 'Limit' =>4550));

if ($rets->NumRows($search) > 0) {
    $fields_order = $rets->SearchGetFields($search);

    while ($record = $rets->FetchRow($search)) {
        foreach ($fields_order as $fo) { 
            if ($fo == 'sysid') { $systemid = $record[$fo] ; }
            if ($fo == '881') { $saddress = isset($record[$fo]) ? $record[$fo] : ""; }
            if ($fo == '214') { $sremarks = isset($record[$fo]) ? $record[$fo] : ""; }
        }

        $porpertytitle = $saddress;

        $args = array(
            'numberposts' => -1,
            'post_type' => 'property',
            'post_status' => 'publish',
            'meta_key' =>'sysid',
            'meta_value' => $systemid
        );

        $the_query = new WP_Query($args);

        if($the_query->have_posts()) {
            while ($the_query->have_posts()) {
                $the_query->the_post();
                unset($systemid);
                unset($args);
            }
        } else {
            $my_listing = array(
                'post_title' => $porpertytitle,
                'post_type' => 'property',
                'post_content' => $sremarks,
                'post_status' => 'publish',
            );

            $listing_post_id = wp_insert_post($my_listing);

            if($listing_post_id > 0) {
                update_post_meta($listing_post_id, 'sysid', $systemid);
            }

            unset($systemid);
            unset($args);
            unset($listing_post_id);
        }
        wp_reset_postdata();
    }
}

      

Sorry for this long code. But I also tried to disable the variables after each loop, but didn't work.

+3


source to share


1 answer


I had a similar seemingly random double problem. Finally, I figured the problem was that the RETS server offsets are 1-based, not zero-based. Also, if you don't go through the offset with your query, the server will sort the results differently than if you did.



If you can check the sent request to the server, see if it includes offset=1

in the first request. If it is zero or absent, you will get duplicates of the first page results scattered on subsequent pages, which can be large with 4000 pages

+2


source







All Articles