How to get featured post title & image using JSON API?

I made a shortcut to my image by adding it directly to the API response. //Add in functions.php, this hook is for my ‘regions’ post type add_action( ‘rest_api_init’, ‘create_api_posts_meta_field’ ); function create_api_posts_meta_field() { register_rest_field( ‘regions’, ‘group’, array( ‘get_callback’ => ‘get_post_meta_for_api’, ‘schema’ => null, ) ); } //Use the post ID to query the image and … Read more

How to loop through JSON data in wordpress WP REST API

I managed to solve this and access the JSON data using a foreach loop: $json = lusso_posts(); #var_dump( $json ); #die(); foreach( $json as $post ) { $titles = $post->title; $images = $post->featured_image->guid; ?> <div class=”lusso-posts”> <div class=”image-container”><img src=”https://wordpress.stackexchange.com/questions/210472/<?php echo $images; ?>” /> </div> <h4><?php echo $titles; ?></h4> </div> <?php }

Include custom post meta value in fetched JSON

Two things to look at: 1) register_meta() 2) custom-fields support on post type. 1) register_meta You can register your custom post meta with show_in_rest => true to make it accessible via the api. Bottom of this page: Modifying Responses, provides the following example: <?php // The object type. For custom post types, this is ‘post’; … Read more

WP JSON list all permalinks

Is it possible to do this with a rest query + filters? eg. http://wpsite.com/wp-json/wp/v2/posts?filter[only-permalinks] Yes, since 4.9.8 (see #43874) it’s possible to render only fields needed with the _fields parameter. Examples Render only the permalinks: https://example.com/wp-json/wp/v2/posts?_fields=link [ { link: “https://example.com/foo/” }, { link: “https://example.com/bar/” }, ] Render only the post IDs and permalinks: https://example.com/wp-json/wp/v2/posts?_fields=id,link [ … Read more

WordPress function that makes HTML safe to be sent via AJAX request

Depending on what kind of HTML you’re expecting, there are different tools you can use: esc_html() escapes entire HTML blocks so you don’t end up with breaking characters in your JSON object literals. esc_html_e() escapes (as above) and translates the string if you’re concerned about localization in that context. wp_kses() will parse the HTML string … Read more