I’ve written plenty about item-oriented PHP and the WordPress REST API for Torque over the past few years. I’ve additionally touched on the usage of Composer for dependency management and to offer an autoloader, as well as covered unit testing. The primary message of the whole thing I’ve written is that through making use of the set up best practices of software improvement to how we expand for WordPress, we are able to create higher plugins.
This is the primary of a series of articles so as to pull together those standards in a practical, functional example. I’ll be on foot through growing a WordPress plugin to regulate the competencies of WordPress REST API endpoints so that they may be better optimized for search. The plugin is available on GitHub. You might also want to browse the commit log to see how I put it together.
In this series, I’ll cowl structuring plugins and training the usage of contemporary object-oriented PHP and not only the way to make it testable, however additionally a way to write computerized assessments for it. I will cowl the distinction between unit tests, integration assessments, and attractiveness checks and display you how to write and automate going for walks each kind. This article begins the series by means of displaying the way to use filters to alter the WordPress REST API the usage of an object-orientated technique.
Improving WordPress Search Using the REST API
Plugins like SearchWP or Relevansi, or integrations with ElasticSearch — a technology that makes use of a completely specific stack than WordPress — using Jetpack or ElasticPress, are frequently used to enhance WordPress seek. These types of plugins provide higher seek effects and often pair nicely with faceted-search interface, that’s exquisite for eCommerce apps.
Search via the WordPress REST API inherits all of those equal troubles and the same solution. In this post, I’m going to begin through searching at how search works via default, and what the constraints are. Then we’ll look at how to regulate the hunt the use of specific methods and combine with SearchWP.
WordPress’s built-in seek capabilities frequently need to be progressed the use of out of doors offerings. While this newsletter is about an object-oriented method to modifying how WordPress REST API routes for posts paintings, the realistic instance could be enhancing search.
When WordPress is used because the again give up for a decoupled front stop together with a local mobile app or net app, probable constructed the usage of Vue or React or Angular, having satisfactory search via the REST API is essential. The code this newsletter covers will assist you if your app’s customers need to discover the proper product variation or seek content with the aid of a complicated set of rules based on multiple taxonomies, and also you’re writing custom code, no longer simply putting in a plugin.
Searching Posts with the WordPress REST API
If you wanted to search for all posts that had been of the put up type “product” on a site, the usage of the search terms “Taco Shirts” you would make a request to the /wp/v2/product?S=Taco+Shirt endpoint. If you wanted to improve the pleasant of the outcomes, the answers I indexed above would help.
As we mentioned above, WP_Query, what the put up endpoints of the WordPress REST API use, is not a tremendous tool for search. More specially, WP_Query, possibly due to its dependence on MySQL, is not as good as specialized search equipment that has a tendency to be built the use of NoSQL databases.
First, let’s take a look at how we will skip WP_Query’s interactions with WordPress’s database if a REST API request is being made.
This is the strategy many search plugins take to alternative the consequences in their very own search systems, for what WP_Query would have generated with the aid of default. The seeking system may additionally use the same database. It can also hook up with some different database, probably through an API request, for example to an ElasticSearch or Apache Solr server.
If you look in WordPress core, you’ll find the clear out “posts_pre_query” runs right earlier than WP_Query queries the database, but after the SQL query has been prepared. This filter returns null with the aid of default. If that price is null, WordPress keeps with its default behavior: querying the WordPress database and returning the outcomes as an easy array of WP_Post objects.
On the other hand, if the return cost of this filter is an array — with a bit of luck containing WP_Post items — then WordPress’s default behavior is not used.
Let’s look at how we can use posts_pre_query to return a ridicule WP_Post. This approach could be very useful for checking out, but a more complicated model of the equal sample can be used to combine a separate database with your WordPress site:
In this situation, where the usage of mock facts, but we can be the use of SearchWP’s query elegance or something else. One other thing to preserve in thoughts approximately this code is it’s going to run on any WP_Query, no longer just a WP_Query object created through the WordPress REST API. Let’s modify that so we don’t use the filter out until it’s far a WordPress REST API request by means of adding conditional good judgment:
Modifying WordPress REST API Endpoints Arguments
We simply looked at a way to trade how the quest outcomes are generated for WordPress REST API requests. That lets in us to optimize our queries for better search, but it’s far likely to expose a need for a different schema for the endpoints.
For instance, what in case you wanted to allow the quest on your products endpoint to optionally allow extra put up kind to be blanketed inside the seek. I protected a one-of-a-kind technique to the identical problem closing yr.