Get WordPress posts from another WordPress website

<?php
$response = wp_remote_get( add_query_arg( array(
		'per_page' => 3
	), 'https://domain.com/wp-json/wp/v2/posts' ) );
	if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
		$posts = json_decode( $response['body'] ); // our posts are here
		
		foreach( $posts as $post ) {
			$date = date('F j, Y', strtotime($post->date));
			echo '<div class="blog-post-date">' . $date . '</div>';
			echo '<h3 class="blog-post-title">'. $post->title->rendered . '</h3>';
			echo $post->content->rendered;
			//$output .= print_r( $post );
		}
	}

Leave a Reply

Your email address will not be published.