简体   繁体   English

Wordpress 如果当前帖子不在 API 中,则将帖子移至垃圾箱

[英]Wordpress move post to trash if current post is not in API

I have created a PHP function that uses an API request.我创建了一个使用 API 请求的 PHP function。 I then create a custom post type and store the values inside the Title, content and meta fields.然后我创建一个自定义帖子类型并将值存储在标题、内容和元字段中。

There is a unique identifier key called "requisitionId".有一个名为“requisitionId”的唯一标识符键。 I store this in a meta field.我将其存储在元字段中。

The import works as expected.导入按预期工作。 The problem I'm having is moving previously imported posts to trash if these are not in the incoming API.我遇到的问题是将以前导入的帖子移到垃圾箱,如果这些帖子不在传入的 API 中。

I'm trying to do this by trying to check if the existing post's "requisitionId" exists in the incoming API. If not move this existing post to trash.我试图通过尝试检查传入的 API 中是否存在现有帖子的“requisitionId”来做到这一点。如果不存在,请将此现有帖子移至垃圾箱。 I'm using the in_array() function to check this.我正在使用 in_array() function 来检查这个。

As it is right now, nothing is moving to trash.就像现在一样,没有任何东西被扔进垃圾桶。

The idea is to run a cron to keep the posts up to date with the API. That is why I need to move the existing posts to trash if they are not currently inline / up tp date with the API results.我的想法是运行一个 cron 来使帖子与 API 保持同步。这就是为什么我需要将现有帖子移动到垃圾桶,如果它们当前不是内联/最新的 API 结果。

What am I doing wrong and how can I improve the code?我做错了什么以及如何改进代码?

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.';
    }
}

I've found a solution.我找到了解决方案。

The problem was that I was checking the incoming API IDs against itself.问题是我正在对照自身检查传入的 API ID。 Instead of checking the existing post IDs against the incoming API IDs.而不是根据传入的 API ID 检查现有的帖子 ID。

I'm also now permanently deleting all the posts before importing the new ones.我现在也在导入新帖子之前永久删除所有帖子。

Here is the updated code:这是更新后的代码:

    $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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM