为什么在从 RETS 服务器获取数据时添加了重复的帖子? [英] Why Duplicate posts are being added while fetching data from RETS server?

查看:15
本文介绍了为什么在从 RETS 服务器获取数据时添加了重复的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHRETS 从 rets 服务器在 wordpress 中添加帖子.不幸的是,正在添加重复的帖子.我已经使用 WP Query 使用元键和值检查现有帖子.当我尝试添加 10 或 50 个帖子时,此代码运行良好,但是当我将限制设置为 4000 时,它开始添加重复的帖子.我已经多次运行此代码并多次刷新数据库.这是一个代码示例:

I am trying to add posts in wordpress from rets server using PHRETS. Unfortunately duplicate posts are being added. I have used the WP Query to check the existing post using meta key and value. This code is running well when I am trying to add 10 or 50 posts but when I set the limit to 4000 it start adding duplicate posts. I have run this code so many time and flushing the database so many time. Here is a code sample:

$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 have also tried unset the variables after every loop but not working.

推荐答案

我自己也遇到了类似的看似随机的重复问题.我最终确定问题在于 RETS 服务器偏移量是基于 1,而不是基于零.此外,如果您没有在请求中传递偏移量,则服务器会对结果进行排序,而您这样做时会有所不同.

I had a similar seemingly random duplicate problem myself. I finally determined the problem is that RETS Server offsets are 1 based, not zero based. Also, if you don't pass an offset with your request the server will sort the results differently than if you do.

如果您可以检查发送到服务器的请求,请查看它是否在第一个请求中包含 offset=1.如果它为零或丢失,您将得到分散在后续页面中的第一页结果的重复,这对于 4000 页大小来说可能很多

If you can inspect the request being sent to the server, see if it includes offset=1 on the first request. If it is zero or missing you'll get repeats of the first page's results scattered in subsequent pages, which can be a lot with a 4000 page size

这篇关于为什么在从 RETS 服务器获取数据时添加了重复的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆