Advanced live search in WordPress 1 – Custom REST API

WordPress is a very decent CMS, but its default search is limited. Here we will start to implement a very advanced ajax search for your WordPress theme without needing plugin. We are going to include Custom Fields in the search results and all the fancy stuff too.

This is the first article in a series. In this article we will go through building a custom REST API. This is a part of implementing advanced live search in WordPress, but you may use this article to build your own REST API, for any other reason you need. But we could use it later as a tool to implement our live search.

Step 1: Easily build a custom REST API

For implementing the advanced search in WordPress, we first need to create a custom API to get the data (search results) from the database. This article is for customising the WordPress REST API and it’s a requirement for implementing live search in WordPress. You don’t need any plugin.

To put it simply, WordPress REST API lets you interact with your site’s data. You basically enter a url and the data will be recieved from the server in the JSON format. But you also could manipulate the data using the REST API, which is not what we need to do here.

Why a developer should use WordPress REST API

The WordPress REST API is a powerful tool that lets you work with WordPress using languages other than PHP or even building a WordPress SPA. Here in this post, the REST API enables us to search in the database on the fly, using JavaScript. But the REST API has many other usages, like adding or manipulating data.

Why we need to customise WordPress REST API

WordPress has default REST APIs. But here we’d prefer to build a custom REST API for 3 main reason:

1- Remove unnecessary data.

2- Include custom fields when searching.

3- Reduce the http requests by combining all of our necessary data in one API or route.

How to search using WordPress REST API

By adding certain parameters to your API you will be able to filter the results.

Here I show you the search ability in APIs. This is an example that we have a parameter we put in the url and also show the results as an example.

How to use the REST API in WordPress live search

When user types something in the search input, we will send an http request using JavaScript, then we recieve the search result in JSON format and then display the search result to the user. All of this would be done on the fly (without reloading the page).

 

How to include the custom fields in search

Here we improve our custom REST API so it include the custom fields in the search too.

Including all of custom fields

By default, WordPress doesn’t include custom fields in its search. Here we add the code to our custom api so it search in all the custom fields too.

Including Multiple custom fields manually

Usually the custom fields include too many fields and most of them are not necessary. So, you’d probab;y want to include some custom fields only.

Including multiple custom fields in a query programmatically

Here we add the custom fields to the search in a more advanced way.

BONUS: Includng multiple custom fields in the search dynamically, using settings on dashboard

Select the custm fields you want to include in the search from dashboard settings. We will use ACF plugin or a simpler way if possible.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

One comment

  1. 85.185.75.76:53
    81.12.38.10:53
    81.12.40.14:53
    81.91.147.3:53
    81.12.70.146:53
    93.115.144.1:53
    87.107.82.213:53
    93.118.140.51:53
    87.107.154.27:53
    91.92.190.126:53
    94.182.154.105:53
    87.107.48.10:53
    94.184.10.133:53
    94.184.10.132:53
    95.38.10.25:53
    91.92.185.232:53
    95.80.164.5:53
    94.183.163.248:53
    93.115.151.163:53
    94.184.10.134:53
    94.184.10.131:53
    95.38.47.173:53
    95.130.60.34:53
    10.160.155.9:53
    37.32.2.2:53
    185.116.163.63:53
    185.191.77.50:53
    185.208.76.101:53
    194.156.140.111:53
    45.140.225.62:53
    2.188.21.130:53
    46.245.66.205:53
    185.141.171.187:53
    45.135.243.61:53
    5.56.132.97:53
    217.218.201.113:53
    5.160.88.54:53
    185.206.229.36:53
    62.220.116.100:53
    217.219.77.194:53
    91.92.214.66:53
    94.184.10.134:53
    78.39.59.246:53
    94.184.10.132:53
    213.207.195.146:53
    94.184.10.133:53
    81.28.253.252:53
    80.210.54.68:53
    89.40.246.59:53
    185.126.5.40:53
    5.160.171.36:53
    94.182.154.105:53
    93.118.140.51:53
    10.104.227.5
    10.104.204.23
    109.109.34.100
    10.92.12.225
    109.109.34.101
    10.249.52.84
    109.109.34.105
    109.109.34.104
    109.125.133.198
    109.109.34.106
    109.201.8.8
    109.230.91.228
    130.185.75.205
    151.233.51.64
    151.233.51.192
    151.233.49.80
    109.238.187.246
    151.233.50.143
    109.95.60.15
    151.233.54.182
    151.233.58.92
    151.233.54.209
    178.252.141.73
    178.131.17.162
    178.252.170.218
    178.252.170.222
    176.101.33.214
    185.106.144.149
    185.109.61.102
    185.112.37.128
    185.126.5.40
    185.126.5.41
    185.120.201.34
    185.132.80.64
    185.113.56.35
    185.129.215.100
    185.129.214.205
    185.134.99.46
    185.137.108.23
    185.161.113.10
    185.172.0.206
    185.145.8.254
    185.140.4.79
    185.206.93.50
    185.208.76.104
    185.215.125.6
    185.208.76.105
    185.208.76.106
    185.237.85.5
    185.53.143.13
    185.231.183.26
    185.44.113.194
    185.66.229.40
    185.66.229.9
    185.49.87.219
    185.49.87.218
    185.74.221.14
    185.72.27.232
    188.121.146.226
    188.121.145.94
    188.213.65.54
    188.75.126.74
    193.228.168.65
    188.136.208.244
    193.228.169.25
    194.31.108.88
    2.180.21.24
    2.184.70.50
    2.188.162.77
    2.188.214.201
    2.188.166.74
    2.189.167.182
    2.189.1.1
    2.189.1.2
    2.189.162.250
    217.219.34.66
    217.219.120.82
    217.219.82.226
    31.214.169.254
    31.47.56.60
    31.47.56.59
    37.148.82.96
    37.148.58.95
    37.148.38.23
    37.148.35.12
    37.255.229.99
    37.255.242.252
    45.81.19.10
    37.255.211.238
    37.98.104.129
    45.92.94.208
    46.100.61.33
    46.209.157.18
    46.209.25.195
    5.160.119.227
    5.160.119.225
    46.249.122.87
    5.160.13.83
    5.160.187.126
    5.160.45.138
    5.160.140.16
    5.160.115.190
    5.160.93.246
    5.190.212.245
    5.160.56.30
    5.160.98.131
    5.202.42.193
    5.202.170.23
    5.202.46.124
    5.202.50.116
    77.104.103.130
    5.202.93.180
    5.202.46.18
    77.238.123.237
    5.233.250.98
    5.202.60.116
    77.104.115.152
    77.237.83.30
    78.38.251.250
    78.39.53.162
    78.38.251.253
    78.38.251.25
    80.210.62.146
    81.12.99.102
    81.31.250.133
    81.90.145.122
    81.29.248.101
    81.12.47.58
    81.12.71.10
    81.16.124.73
    81.12.90.74
    81.12.124.20
    81.12.70.14
    84.241.25.187
    85.133.162.44
    85.133.160.236
    85.133.160.240
    85.185.159.74
    85.185.159.77
    88.218.16.4
    85.9.121.3
    87.107.49.1
    89.32.248.145
    89.32.251.65
    89.40.246.59
    91.92.207.127
    91.243.160.207
    91.243.160.62
    91.232.74.194
    92.246.147.81
    93.118.125.145
    93.126.3.34
    93.126.19.246
    93.126.29.44
    93.126.19.125
    94.182.18.137
    93.126.2.252
    93.126.29.64
    94.183.163.76
    94.183.51.107
    95.38.11.94
    95.80.164.10
    95.80.164.5
    95.80.164.6
    95.38.47.170
    95.38.47.174
    95.38.47.172
    10.104.36.24
    10.106.241.13
    10.104.208.247
    10.104.229.111
    10.133.8.2
    10.138.206.18
    10.138.206.12
    10.223.252.141
    10.143.91.46
    10.133.8.6
    10.249.52.244
    10.250.252.15
    10.249.52.58
    10.223.252.145
    10.249.52.137
    10.249.52.239
    10.75.10.3
    109.109.34.102
    109.109.34.103
    109.125.130.50
    109.238.188.148
    176.101.32.2
    95.38.209.230
    178.239.156.120
    178.252.143.133
    95.38.209.229
    185.124.113.224
    185.158.172.116
    185.208.76.101
    185.225.240.138
    185.46.111.122
    185.46.111.74
    185.53.142.204
    185.49.96.223
    193.186.32.32
    193.228.169.225
    193.228.168.209
    193.228.168.162
    2.187.35.15
    2.188.162.76
    2.188.162.74
    2.188.162.78
    2.189.1.15

Leave a comment

Your email address will not be published. Required fields are marked *