Issue
I have created a PHP function that uses an API request. 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". 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.
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. I'm using the in_array() function to check this.
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.
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.';
}
}
Solution
I've found a solution.
The problem was that I was checking the incoming API IDs against itself. Instead of checking the existing post IDs against the incoming API IDs.
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;
Answered By - Ben de Meillon
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.