首页 > 解决方案 > 如何使用 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;
   ?>

标签: phpwordpressapiposts

解决方案


推荐阅读