php - 如何使用 php 从 wordpress 中的 api 更新帖子?
问题描述
我目前正在从 api 中提取信息并通过 php 在 wordpress 中创建帖子。目前它会提取信息并成功创建帖子。我现在遇到的问题是,当用户从不是 wordpress 的网站进行更改时,wordpress 上的帖子不会更新。
任何帮助将不胜感激。
到目前为止,这是我的代码。
//Fetching data from api
?php
require_once('../../../../wp-load.php');
$jobs = wp_remote_get( 'https://careerconnect.recruitee.com/api/offers');
$jobs_decode = json_decode( $jobs['body'], true);
$jobList = $jobs_decode['offers'];?>
<?php
// Loop through each job from the API.
foreach ( $jobList as $job ):
$job_title = $job['title'];
$job_ID = $job['id'];
// Added more data
$job_salary = $job['salary'];
$job_description = $job['description'];
$job_location = $job['location'];
// Prep our query to search for the existing job.
$args = [
'posts_per_page' => 1,
'post_type' => 'jobs',
// Account for people who set a status on posts other than published
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future',
'private', 'inherit' ),
'meta_query' => [
[
'key' => 'job_id',
'value' => $job['id'],
],
],
];
// Perform the search, if the job exists, a post object will return, else null.
$existing_job = get_posts($args);
// Update our existing job ID if it exists.
if ($existing_job) {
echo 'Job ID ' . $job_ID . ' exhists' . '<br>';
}else{
$post_id = wp_insert_post([
'post_type' => 'jobs',
'post_title' => $job_title,
'post_status' => 'publish',
]);
update_field( 'job_id', $job_ID, $post_id );
update_field( 'job_salary', $job_ID, $post_id );
update_field( 'job_content', $job_description, $post_id );
update_field( 'job_location', $job_location, $post_id );
echo 'Job ID ' . $job_ID . ' created' . '<br>';
}
endforeach;
?>
解决方案
推荐阅读
- python - 如何将管道中的预处理数据转换为数据帧?
- r - 删除由`unlist()`添加的点,并在R中的字符串向量中将其替换为“/”
- javascript - 如何使用 ajax 使用按钮打印另一页
- android - 如何使用 Dagger Hilt 运行 android-testify 测试?
- telegram - @client.on(events.NewMessage 不工作:(
- visual-c++ - 将 COM 接口智能指针传递给函数时出现警告 C26415 / C26418
- javascript - Typescript:如何向 React Alert 中的严重性属性添加类型声明?
- sql - 将值更改为不为空后的 ALTER TABLE 选项无效
- vue.js - 在“vue”中找不到导出“默认”(导入为“vue”)
- javascript - 快速会话,一段时间后销毁会话?