Notes

Adding a Custom Endpoint to WordPress

Edit on GitHub

ReactJS
 1add_action( 'rest_api_init', function () {
 2	register_rest_route( 'fpp/v1', 'news', array(
 3		'methods' => 'GET',
 4		'callback' => 'get_news_listing'
 5	));
 6});
 7
 8function get_news_listing ( $data ) {
 9	$post_args = array(
10		'numberposts' => 10,
11		'post_type' => 'post'
12	);
13
14	$posts = get_posts($args);
15
16	if ( empty( $posts ) ) {
17		return null;
18	}
19
20	foreach($posts as $key => $value) {
21	  $posts[$key] = $value;
22	}
23
24	// Add thumbnail to each post object
25	// https://www.php.net/manual/en/function.array-walk.php
26
27	// https://www.php.net/manual/en/control-structures.foreach.php
28
29	return $posts;
30
31}