首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果当前帖子不在API中,则将Wordpress的帖子移到垃圾中

如果当前帖子不在API中,则将Wordpress的帖子移到垃圾中
EN

Stack Overflow用户
提问于 2022-02-25 07:36:31
回答 1查看 119关注 0票数 -1

我已经创建了一个使用API请求的PHP函数。然后创建自定义post类型,并将值存储在标题、内容和元字段中。

有一个名为"requisitionId“的唯一标识符键。我把这个储存在一个元字段里。

进口如预期的那样工作。我遇到的问题是,如果之前导入的帖子不在传入的API中,则将其移到垃圾中。

我试图通过检查传入API中现有post的"requisitionId“是否存在来实现这一点。如果没有,则将现有的帖子移到垃圾中。我使用in_array()函数来检查这一点。

就像现在一样,没有什么东西会变成垃圾。

这样做的目的是运行一个cron,使文章与API保持一致。这就是为什么我需要将现有的帖子移到垃圾中,如果它们当前不是与API结果内联/向上的tp日期。

我做错了什么,如何改进代码?

代码语言:javascript
复制
foreach ( $jobs as $job ) {
    $jobs_count++;
    $job_role = ( $job['internalOnly'] == false ) ? 'External' : 'Internal'; 
    $job_title = $job['title'];
    $job_apply_link = $job['applyLink'];
    $job_department = $job['category'];
    $job_city = $job['locationCity'];
    $job_update_uf = $job['lastUpdatedDate'];
    $job_update = date('Y-m-d H:i:s', substr($job_update_uf, 0, 10));
    $job_requisition_id = $job['requisitionId'];
    $job_description_raw = $job['description'];
    $job_description = preg_replace('/ style=("|\')(.*?)("|\')/','',$job_description_raw);
    $job_post_title = $job_title;
    $job_slug = sanitize_title( $job['title'] . '-' . $job_city . '-' . $job_requisition_id );
    $job_post_category = sanitize_title( $job_department );

    $existing_job = get_page_by_path( $job_slug, 'OBJECT', $post_type = 'jobs' );

    $existing_job_id = $existing_job->ID;
    $existing_job_timestamp = $existing_job->post_date;

    $existing_job_requisition_id = get_post_meta( $existing_job_id, 'job-requisition-id', true );

    $job_ids_array = [];

    $job_ids_array[] = $job_requisition_id;

    //CREATE JOB
    $post = array(
        'post_title' => $job_post_title,
        'post_name' => $job_slug,
        'post_content' => $job_description,
        'post_date' => $job_update,
        'post_status' => 'publish',
        'post_type' => 'jobs',
        'meta_query' => array(
            array(
                'key'     => 'job-requisition-id',
                'value'   => $job_requisition_id,
                'compare' => '!=',
            ),
        ),
    );

    if ( $job_role == 'External' ) {
        if ( $existing_job == null ) {
            $post_id = wp_insert_post( $post );
            update_post_meta( $post_id, 'job-apply-link', $job_apply_link );
            update_post_meta( $post_id, 'job-published', $job_update );
            update_post_meta( $post_id, 'job-requisition-id', $job_requisition_id );
            update_post_meta( $post_id, 'job-role', $job_role );
            wp_set_object_terms( $post_id, $job_region, 'jobs-region' );
            wp_set_object_terms( $post_id, $job_city, 'jobs-city' );
            wp_set_object_terms( $post_id, $job_department, 'jobs-department' );

            $success_msg = $job_region . ' jobs imported. Please reload the page.'; 

        } else if ( ( $job_update > $existing_job_timestamp ) && ( $existing_job_requisition_id == $job_requisition_id ) ) {
            $update_jobs_args = array(
                'ID' => $existing_job_id,
                'post_title' => $job_post_title,
                'post_name' => $job_slug,
                'post_content' => $job_description,
                'post_date' => $job_update,
                'post_status' => 'publish',
                'post_type' => 'jobs',
            );
            wp_update_post( $update_jobs_args );
            update_post_meta( $post_id, 'job-apply-link', $job_apply_link );
            update_post_meta( $post_id, 'job-requisition-id', $job_requisition_id );
            update_post_meta( $post_id, 'job-role', $job_role );
            wp_set_object_terms( $post_id, $job_region, 'jobs-region' );
            wp_set_object_terms( $post_id, $job_city, 'jobs-city' );
            wp_set_object_terms( $post_id, $job_department, 'jobs-department' );
            
            $success_msg = $job_region . ' Jobs updated. Please reload the page.';
                    
        } else if ( !in_array( $existing_job_requisition_id, $job_ids_array ) ) {
            // MOVE TO TRASH IF JOBS NO LONGER EXIST ON API
            $jobs_trash_args = array(
                'ID' => $existing_job_id,
                'post_status' => 'trash',
                'post_type' => 'jobs',
                'meta_query' => array(
                    array(
                        'key'     => 'job-requisition-id',
                        'value'   => $job_requisition_id,
                        'compare' => '=',
                    ),
                ),
            );
            wp_update_post( $jobs_trash_args );

            // IMPORT JOBS THAT DOES NOT EXIST
            $post_id = wp_insert_post( $post );
            update_post_meta( $post_id, 'job-apply-link', $job_apply_link );
            update_post_meta( $post_id, 'job-published', $job_update );
            update_post_meta( $post_id, 'job-requisition-id', $job_requisition_id );
            update_post_meta( $post_id, 'job-role', $job_role );
            wp_set_object_terms( $post_id, $job_region, 'jobs-region' );
            wp_set_object_terms( $post_id, $job_city, 'jobs-city' );
            wp_set_object_terms( $post_id, $job_department, 'jobs-department' );

            $success_msg = 'Some new jobs were imported and some moved to trash.';
        }
    } else {
        $success_msg = 'There were no jobs to import. ' . $job_region . ' jobs are up to date.';
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-28 11:59:16

我找到了解决办法。

问题是,我正在检查传入的API ID与其自身。而不是根据传入的API ID检查现有的post ID。

在导入新帖子之前,我还将永久删除所有的帖子。

以下是更新的代码:

代码语言:javascript
复制
    $jobs = $response['requisitions'];
$region = $_POST['region'];
$jobs_count = 0;
$success_msg = '';

$job_args = [
    'post_type' => 'jobs',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'tax_query' => [
        [
            'taxonomy' => 'jobs-region',
            'field'    => 'slug',
            'terms'    => $region,
        ]
    ]
];
$jobs_list = get_posts($job_args);
$jobs_deleted = count($jobs_list);

if ($jobs == null) {
    $success_msg = 'There are no open jobs for '.$job_region;
} else {
    foreach ($jobs as $job) {
        $job_role = ($job['internalOnly'] == false) ? 'External' : 'Internal'; 
        $job_title = $job['title'];
        $job_apply_link = $job['applyLink'];
        $job_department = $job['category'];
        $job_city = $job['locationCity'];
        $job_update_uf = $job['lastUpdatedDate'];
        $job_update = date('Y-m-d H:i:s', substr($job_update_uf, 0, 10));
        $job_requisition_id = $job['requisitionId'];
        $job_description_raw = $job['description'];
        $job_description = preg_replace('/ style=("|\')(.*?)("|\')/','',$job_description_raw);
        $job_post_title = $job_title;
        $job_slug = sanitize_title($job['title'].'-'.$job_city.'-'.$job_requisition_id);
        $job_post_category = sanitize_title($job_department);

        $existing_job = get_page_by_path($job_slug, 'OBJECT', 'jobs');

        $existing_job_id = $existing_job->ID;
        $existing_job_timestamp = $existing_job->post_date;

        $existing_job_requisition_id = get_post_meta($existing_job_id, 'job-requisition-id', true);

        $job_ids_array = [];

        $job_ids_array[] = $job_requisition_id;

        //CREATE JOB
        $args = [
            'post_title' => $job_post_title,
            'post_name' => $job_slug,
            'post_content' => $job_description,
            'post_date' => $job_update,
            'post_status' => 'publish',
            'post_type' => 'jobs',
            'meta_input' => [
                'job-apply-link'   => $job_apply_link,
                'job-published'   => $job_update,
                'job-role'   => $job_role,
                'job-requisition-id'   => $job_requisition_id,
            ],
        ];

        if ($job_role == 'External') {
            $jobs_count++;
            if (empty($jobs_list)) {
                $post_id = wp_insert_post($args);
                wp_set_object_terms($post_id, $job_region, 'jobs-region');
                wp_set_object_terms($post_id, $job_city, 'jobs-city');
                wp_set_object_terms($post_id, $job_department, 'jobs-department');
    
                $success_msg = $jobs_count.' '.$job_region.' jobs imported. Please reload the page.'; 
    
            } else if (!empty($jobs_list)) {
                
                foreach ($jobs_list as $item) {
                    wp_delete_post($item->ID, true);
                }
    
                $post_id = wp_insert_post($args);
    
                wp_set_object_terms($post_id, $job_region, 'jobs-region');
                wp_set_object_terms($post_id, $job_city, 'jobs-city');
                wp_set_object_terms($post_id, $job_department, 'jobs-department');
    
                $success_msg = $jobs_deleted;
                $success_msg .= ' '.$job_region.' jobs deleted and ';
                $success_msg .= $jobs_count.' '.'imported. Please reload the page.';
    
            } else if (
                ($job_update > $existing_job_timestamp) &&
                ($existing_job_requisition_id == $job_requisition_id)) {
                $update_jobs_args = [
                    'ID' => $existing_job_id,
                    'post_title' => $job_post_title,
                    'post_name' => $job_slug,
                    'post_content' => $job_description,
                    'post_date' => $job_update,
                    'post_status' => 'publish',
                    'post_type' => 'jobs',
                ];
                wp_update_post($update_jobs_args);
                update_post_meta($post_id, 'job-apply-link', $job_apply_link);
                update_post_meta($post_id, 'job-requisition-id', $job_requisition_id);
                update_post_meta($post_id, 'job-role', $job_role);
                wp_set_object_terms($post_id, $job_region, 'jobs-region');
                wp_set_object_terms($post_id, $job_city, 'jobs-city');
                wp_set_object_terms($post_id, $job_department, 'jobs-department');
                
                $success_msg = $job_region;
                $success_msg .= ' Jobs updated. Please reload the page.';
                        
            }
        } else {
            $success_msg = 'There were no jobs to import. '.$job_region.' jobs are up to date.';
        }
    }
}

echo  $success_msg;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71262511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档