admin管理员组

文章数量:1122832

I am currently trying to create a Wordpress plugin which create posts in the Wordpress database based on data from an external JSON API. As an example this NewsAPI feed could be used:

;apiKey=81143272da6c48d58bc38fe80dd110d6

The plugin I have written decodes the JSON data by using json_decode and loops through the article object in the JSON feed. Finally, the posts is being inserted programmatically using wp_insert_post:

<?php

/**
* Plugin Name: Automatic News Feed Importer
* Version: 1.0.0
*/

function news_importer() {
$url = ";apiKey=81143272da6c48d58bc38fe80dd110d6";
$response = wp_remote_get( $url );
$data = json_decode( wp_remote_retrieve_body( $response ) );

foreach( $data->articles as $news ) {
    $post_title = $news->title;
    $post_content = $news->content;
    $post_date = $news->publishedAt;
    
    $post = array(
        'post_title' => $post_title,
        'post_content' => $post_content,
        'post_date' => $post_date,
        'post_status' => 'publish',
        'post_type' => 'post'
    );
    
    wp_insert_post( $post );

    }
}

My problem is that when the plugin is activated, no posts are creating and added to the database. When a new post is uploaded and appearing in the feed it has to be uploaded automatically (asynchronously).

Any ideas on why this isn't working?

I am currently trying to create a Wordpress plugin which create posts in the Wordpress database based on data from an external JSON API. As an example this NewsAPI feed could be used:

https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=81143272da6c48d58bc38fe80dd110d6

The plugin I have written decodes the JSON data by using json_decode and loops through the article object in the JSON feed. Finally, the posts is being inserted programmatically using wp_insert_post:

<?php

/**
* Plugin Name: Automatic News Feed Importer
* Version: 1.0.0
*/

function news_importer() {
$url = "https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=81143272da6c48d58bc38fe80dd110d6";
$response = wp_remote_get( $url );
$data = json_decode( wp_remote_retrieve_body( $response ) );

foreach( $data->articles as $news ) {
    $post_title = $news->title;
    $post_content = $news->content;
    $post_date = $news->publishedAt;
    
    $post = array(
        'post_title' => $post_title,
        'post_content' => $post_content,
        'post_date' => $post_date,
        'post_status' => 'publish',
        'post_type' => 'post'
    );
    
    wp_insert_post( $post );

    }
}

My problem is that when the plugin is activated, no posts are creating and added to the database. When a new post is uploaded and appearing in the feed it has to be uploaded automatically (asynchronously).

Any ideas on why this isn't working?

Share Improve this question asked Feb 3, 2023 at 16:28 JoachimJoachim 213 bronze badges 3
  • 2 Are you sure you're getting data back from the server? I tried your URL and got an error. (Also, if that's your personal API key, you'll probably want to get it changed, since these questions are publicly visible.) – Pat J Commented Feb 3, 2023 at 16:56
  • 2 How are you calling news_importer()? You've defined it in your plugin, where are calling it from? – darrinb Commented Feb 3, 2023 at 21:12
  • 1 I agree with @Pat and @darrinb. Are you sure the API actually returned a valid data containing the articles array - have you confirmed $data->articles is set, and that news_importer() and wp_insert_post() were both called? Also, you could try setting the 2nd parameter to true (e.g. $post_id = wp_insert_post( $post, true );), and check if an error is returned - see example here. – Sally CJ Commented Feb 4, 2023 at 3:09
Add a comment  | 

2 Answers 2

Reset to default 0

By checking if the 'products' object exists in an if statement and looping through the objects (as in the code above), the function seems to be working:

function json_to_post() {
  $response = wp_remote_get('https://dummyjson.com/products');
  $json_data = wp_remote_retrieve_body($response);
  $data = json_decode($json_data, true);

  if (isset($data['products'])) {
  $products = $data['products'];
  foreach ($products as $post) {
    $title = $post['title'];
    $content = $post['description'];

    $new_post = array(
      'post_title' => $title,
      'post_content' => $content,
      'post_status' => 'publish',
      'post_type' => 'post'
  );
    wp_insert_post($new_post);
  }
 }
}
add_action('init', 'json_to_post');

There's a ready built plugin that does exactly this:

https://wpgetapi.com/downloads/custom-post-import/

本文标签: plugin developmentProgrammatically creating posts based on external JSON feed (asynchronously)