1

i create a custom REST API to get posts

add_action( 'rest_api_init', 'custom_api_get_all_posts' ); 



function custom_api_get_all_posts() {
    register_rest_route( 'custom/v1', '/all-posts', array(
        'methods' => 'GET',
        'callback' => 'custom_api_get_all_posts_callback'
    ));
}

function custom_api_get_all_posts_callback( $request ) {
    // Initialize the array that will receive the posts' data. 
    $posts_data = array();
    // Receive and set the page parameter from the $request for pagination purposes
    $paged = $request->get_param( 'page' );
    $paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1; 
    // Get the posts using the 'post' and 'news' post types
    $posts = get_posts( array(
            'paged' => $paged,
            'post__not_in' => get_option( 'sticky_posts' ),
            'posts_per_page' => 10,            
            'post_type' => listing,
            'tax_query' => array(
                array (
                'taxonomy' => 'flux_rss',
                'field' => 'slug',
                'terms' => 'oui',
                )
            ), 
        )
    ); 


// Loop through the posts and push the desired data to the array we've initialized earlier in the form of an object
foreach( $posts as $post ) {

    $id = $post->ID; 
    $post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;

    $posts_data[] = (object) array( 
        'id' => $id, 
        'title' => $post->post_title,
        'excerpt' => wp_json_encode($post->post_excerpt), 
        'link' => $post->guid,
        'featured_img_src' => $post_thumbnail
    );
}                  
return $posts_data;                   

}

But i get some special character in the response so how i can transform json response to utf8

[{
    "id": 2087,
    "title": "Avec l\u2019ONPA",
    "excerpt": "\"Avec cette association cr\\u00e9\\u00e9e en 1971, les s\\u00e9niors de 55 ans et plus, en activit\\u00e9 professionnelle ou non, et vivant \\u00e0 domicile, m\\u00e8nent une vie active...\"",
    "link": "http:\/\/localhost\/focus\/?post_type=listing&p=2087",
    "featured_img_src": "http:\/\/localhost\/focus\/wp-content\/uploads\/2018\/05\/avec-lonpa.jpg"
},
  • Some notes, your tax query could be replaced with 'flux_rss' => 'oui', and kudos for doing the smart thing and using terms instead of post meta, big performance gains :) But, post__not_in is very expensive, and you should use WP_Query as its default parameters are a little faster. Additionally, listing needs quotes around it, and you should set the post_status which will speed it up a little – Tom J Nowell May 18 '18 at 12:42
2

The problem is here:

'excerpt' => wp_json_encode($post->post_excerpt), 

That array will get passed through wp_json_encode by the REST API, so it gets double encoded, so remove the wp_json_encode here, and it'll work out fine, it's just encoding, it's not the literal human readable value.

E.g. put it in your browsers dev console and output the result, and you'll see it's a non-issue:

enter image description here

As you can see, was encoded as \u2019O because 20190 is the unicode character code of , and HTML can't natively use those characters without some form of encoding. That's why when you inspect HTML, you'll see & but when you view it in the browser you see &, the same thing is happening here

|improve this answer|||||
  • thank u for ur response but u can see we have the same with "title": "Avec l\u2019ONPA", and with the "link" he add "#038;" after the "&" in the link – Ahmad Zammali May 18 '18 at 13:13
  • it'll be decoded at the other end, notice how it was double escaping, \\ rather than \ , how are you testing it? Are you just looking at the output in the HTML? Or are you checking the value javascript gets? The values will be encoded entransit, so once you run it through JSON.parse those character codes will be turned into the real characters – Tom J Nowell May 18 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.