0

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?

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
    Feb 3 at 16:56
  • 2
    How are you calling news_importer()? You've defined it in your plugin, where are calling it from?
    – darrinb
    Feb 3 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
    Feb 4 at 3:09

2 Answers 2

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');
3
  • Can you instead add this as an additional info in your post/question? And add other details like, 1) How and when do you call news_importer() - have you confirmed the function is actually being called? 2) What exactly is the error the API in question returned? 3) Have you tried setting the $wp_error parameter to true - see my comment on your question. As for your code in this answer, though, if news_importer did run, then the problem was likely that post_category must be an array of category IDs.
    – Sally CJ
    Feb 4 at 13:51
  • Hi Sally. Thank you very much for your feedback. By using your suggestions, I have managed to come up with a solution which seems to be working. Kindly see the edited answer above.
    – Joachim
    Feb 4 at 16:23
  • Yes, that looks fine now, but you could do if ( ! is_array( $data ) || empty( $data['products'] ) ) { return; } foreach ... instead of if ( isset( $data['products'] ) ) { foreach ... }. However, this answer didn't answer your question.. But validating a data and checking for errors is always good, e.g. don't simply access the products without first validating it's set. Also, be sure to check the arguments accepted by wp_insert_post(), e.g. supply an array if the argument expects an array. And lastly, I assumed init was used just for testing...
    – Sally CJ
    Feb 4 at 16:47
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.