Working with Multiple Post Types
Explore how to extend search functionality across multiple WordPress post types including posts, pages, courses, and events. Learn to use promise.all to manage multiple REST API requests, combine results for display, and implement error handling to provide seamless user experience in your WordPress theme.
We'll cover the following...
At the moment our search only works with the event post type because in the fetch method, we have hard coded the URL to be /wp-json/wp/v2/event?search=.
sendSearchRequest(){fetch(siteData.root_url+ '/wp-json/wp/v2/event?search='+this.searchInput.value).then((response)=>{}).then((data)=>{});
We want posts, pages, courses, and teachers to be included in the search. To look for pages we need to append /wp-json/wp/v2/pages at the end of the root URL. If we use this URL in the fetch method, we will be able to search for the About Us or Contact Us pages. But now we won’t be able to search for event posts.
To solve this problem, we can send the request to multiple search ...