我創建了一個使用 API 請求的 PHP 函式。然后我創建一個自定義帖子型別并將值存盤在標題、內容和元欄位中。
有一個稱為“requisitionId”的唯一識別符號鍵。我將其存盤在元欄位中。
匯入按預期作業。我遇到的問題是將以前匯入的帖子移動到垃圾箱,如果這些帖子不在傳入的 API 中。
我試圖通過檢查傳入 API 中是否存在現有帖子的“requisitionId”來做到這一點。如果不將此現有帖子移至垃圾箱。我正在使用 in_array() 函式來檢查這一點。
就像現在一樣,沒有任何東西會變成垃圾。
這個想法是運行一個 cron 來使帖子與 API 保持同步。這就是為什么我需要將現有帖子移動到垃圾箱,如果它們當前沒有與 API 結果行內/更新。
我做錯了什么,如何改進代碼?
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.';
}
}
uj5u.com熱心網友回復:
我找到了解決方案。
問題是我正在對照自身檢查傳入的 API ID。而不是根據傳入的 API ID 檢查現有的帖子 ID。
我現在也在匯入新帖子之前永久洗掉所有帖子。
這是更新的代碼:
$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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/434135.html
