php - Wordpress: Inserting Posts from a JSON feed
Get the solution ↓↓↓I have a file: data.php in my twentyfourteen theme directory which receives data from a JSON feed and then inputs it into the wordpress database.
The file is as follows:
<?php
require(dirname(__FILE__)."/../../../wp-config.php");
$json_feed = "http://digitalrand.net/api/url_data/?key=********&pass=********%";
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);
foreach($obj as $article_array){
$url = $article_array['url'];
$title = $article_array['title'];
$category = $article_array['category'];
$large_summary = $article_array['summary'];
$sum = implode(',',$large_summary);
$post = array(
'post_title' => [$title],
'post_content' => [$sum],
'post_status' => ['publish'],
'post_type' => ['post'],
'comment_status' => ['closed'],
'post_template' => ['content.php']
);
wp_insert_post ($post, $wp_error);
}
?>
The above code throws an error: Warning: strtolower() expects parameter 1 to be string, array given in C:\wamp\www\abacus\wp-includes\formatting.php on line 979
I want the data to be input into the wordpress db as soon as it is received so that it can be included in the loop and displayed on the home page of my site.
Answer
Solution:
$post = array(
'post_title' => [$title],
'post_content' => [$sum],
'post_status' => ['publish'],
'post_type' => ['post'],
'comment_status' => ['closed'],
'post_template' => ['content.php']
);
Is not correct, it should be
$post = array(
'post_title' => $title,
'post_content' => $sum,
'post_status' => 'publish',
'post_type' => 'post',
'comment_status' => 'closed',
'post_template' => 'content.php'
);
Check here how its done
https://codex.wordpress.org/Function_Reference/wp_insert_post
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.