投稿が公開された後、WordPress API Restが提供する正確な構造を使用して、すべてのブログ投稿を取得してjsonファイルに保存する必要があります。 WordPress:投稿を公開した後にAPI JSONを保存する
add_action('publish_post', function($ID, $post) {
$url = 'http://website.local/wp-json/wp/v2/posts/';
$response = wp_remote_get($url);
file_put_contents('data.json', $response);
}, 10, 2);
しかし、それはエラーを返すだ:だから私は、これを使用してい
Catchable fatal error: Object of class Requests_Utility_CaseInsensitiveDictionary could not be converted to string in /var/www/public/wp-content/themes/twentyseventeen/functions.php on line 578
file_put_contentsを呼び出すと、print_r($応答)を行い、出力が何かを確認できますか? $ responseはオブジェクトで、文字列として解釈する方法はわかりません。 – victor
$ response = wp_remote_get($ url); jsondecodeを実行して2番目のパラメータをtrueに設定するよりjson_decode($ response、true); –
@Aki実際、json_encodeを実行する必要があります。先端に感謝します。 – marcelo2605