• Resolved divya96

    (@divya96)


    Hi..
    We recently redesigned and migrated our WordPress site to a new server. Before this transition, our custom mobile app could seamlessly fetch and display WordPress posts using the REST API. However, since completing the redesign and migration process, the posts no longer appear in our mobile app.

    the REST API was added to WordPress core starting from version 4.7 itself, current version of wordpress is 6.4.3. So no need of installation of plugin here.

    JSON response for url: https://creativeedu.in/wp-json/

    JSOn response for url : https://creativeedu.in/wp-json/wp/v2/posts

    Both showing JSON responses, that means API is enabled..

    I’ve gone through the steps outlined above, but the issue persists. Can anyone provide further guidance on how to solve this?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter divya96

    (@divya96)



    Display Recent WordPress Posts

    We are using this code

    <script>
        // URL of your WordPress site's REST API endpoint for posts
        const apiUrl = 'https://creativeedu.in/wp-json/wp/v2/posts?per_page=4&_embed';
    
        // Element where you want to display the posts
        const postsContainer = document.getElementById('posts-container');
    
        // Function to fetch and display recent WordPress posts
        async function fetchAndDisplayRecentPosts() {
            try {
                const response = await fetch(apiUrl);
                const posts = await response.json();
    
                // Loop through the recent posts and create HTML elements
                posts.forEach(post => {
                    const postElement = document.createElement('div');
                    postElement.innerHTML = `
                        <h2>${post.title.rendered}</h2>
                        <p>${post.content.rendered}</p>
                        <img src="${post._embedded['wp:featuredmedia'][0].source_url}" alt="${post.title.rendered}" />
                    `;
                    postsContainer.appendChild(postElement);
                });
            } catch (error) {
                console.error('Error fetching posts:', error);
            }
        }
    
        // Call the function to fetch and display recent posts
        fetchAndDisplayRecentPosts();
    </script>

    The script causes a JavaScript error because a field expected by the script does not (or no longer) exist in the return of the API. The message is:

    Error fetching posts: TypeError: post._embedded['wp:featuredmedia'] is undefined

    The _embedded property still exists. However, the entry “wp:featuredmedia” does not. I would recommend that you check this in your code. How to do this is described here, for example: https://stackoverflow.com/questions/48916466/wp-api-and-js-cannot-read-property-wpfeaturedmedia-of-undefined

    Thread Starter divya96

    (@divya96)

    Thanks to your help, the issue has been fixed!

    Here’s the updated code.



    Display Recent WordPress Posts

    <script>
        const apiUrl = 'https://creativeedu.in/wp-json/wp/v2/posts?per_page=4&_embed';
        const postsContainer = document.getElementById('posts-container');
    
        async function fetchAndDisplayRecentPosts() {
            try {
                const response = await fetch(apiUrl);
                if (!response.ok) {
                    throw new Error('Failed to fetch posts');
                }
                const posts = await response.json();
    
                // Loop through the recent posts and create HTML elements
                posts.forEach(post => {
                    const postElement = document.createElement('div');
    
                    const title = document.createElement('h2');
                    title.textContent = post.title.rendered;
                    postElement.appendChild(title);
    
                    const content = document.createElement('p');
                    content.textContent = post.content.rendered;
                    postElement.appendChild(content);
    
                    // Check if featured media is available
                    if (post._embedded && post._embedded['wp:featuredmedia'] && post._embedded['wp:featuredmedia'][0].source_url) {
                        const image = document.createElement('img');
                        image.src = post._embedded['wp:featuredmedia'][0].source_url;
                        image.alt = post.title.rendered;
                        postElement.appendChild(image);
                    }
    
                    postsContainer.appendChild(postElement);
                });
            } catch (error) {
                console.error('Error fetching or displaying posts:', error);
            }
        }
    
        fetchAndDisplayRecentPosts();
    </script>

    Yes, that looks better. Nice if I could help. You are welcome to set the topic to solved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘ssue with WordPress REST API: Posts Not Visible in?Mobile?App’ is closed to new replies.