我已经创建了一个使用API请求的PHP函数。然后创建自定义post类型,并将值存储在标题、内容和元字段中。
有一个名为"requisitionId“的唯一标识符键。我把这个储存在一个元字段里。
进口如预期的那样工作。我遇到的问题是,如果之前导入的帖子不在传入的API中,则将其移到垃圾中。
我试图通过检查传入API中现有post的"requisitionId“是否存在来实现这一点。如果没有,则将现有的帖子移到垃圾中。我使用in_array()函数来检查这一点。
就像现在一样,没有什么东西会变成垃圾。
这样做的目的是运行一个cron,使文章与API保持一致。这就是为什么我需要将现有的帖子移到垃圾中,如果它们当前不是与API结果内联/向上的tp日期。
我做错了什么,如何改进代码?
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.';
}
}发布于 2022-02-28 11:59:16
我找到了解决办法。
问题是,我正在检查传入的API ID与其自身。而不是根据传入的API ID检查现有的post 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://stackoverflow.com/questions/71262511
复制相似问题