FacetWP REST API
FacetWP has two API endpoints: the /facetwp/v1/fetch
and the /facetwp/v1/refresh
endpoint. The /facetwp/v1/fetch
endpoint lets you query raw FacetWP data, with which you can build custom applications. The /facetwp/v1/refresh
endpoint is used by FacetWP itself in the front-end, for filtering.
By default, the fetch
endpoint is disabled. The /facetwp/v1/refresh
endpoint is always open.
Enable the fetch API endpoint
By default, the REST API /facetwp/v1/fetch
endpoint is disabled. To enable it, use this hook in your (child) theme’s functions.php:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_api_can_access', function( $boolean ) { return current_user_can( 'manage_options' ); });
Fetch endpoint URL
/wp-json/facetwp/v1/fetch
The /facetwp/v1/fetch
endpoint expects a POST
request with 1 parameter: data
. This parameter should be a string (a stringified JSON object).
Example
Let’s retrieve cars
with make = audi
, and also grab choices for the vehicle_type
facet.
Here’s the request JSON, before getting stringified:
How to use custom JavaScript code?
JavaScript code can be placed in your (child) theme's main JavaScript file. Alternatively, you can add it manually between
<script>
tags in the<head>
section of your (child) theme's header.php file. You can also load it with a hook in your (child) theme's functions.php file, or in the Custom Hooks add-on. To load the code only on pages with facets, use thefacetwp_scripts
hook. To load it on all pages, usewp_head
orwp_footer
. Or you can use a code snippets plugin. More infovar request_data = { 'data': { 'facets': { 'make': ['audi'], 'vehicle_type': [] }, 'query_args': { 'post_type': 'cars', 'posts_per_page': 4, 'paged': 1 } } };
Here’s the request using JavaScript’s fetch()
method:
How to use custom JavaScript code?
JavaScript code can be placed in your (child) theme's main JavaScript file. Alternatively, you can add it manually between
<script>
tags in the<head>
section of your (child) theme's header.php file. You can also load it with a hook in your (child) theme's functions.php file, or in the Custom Hooks add-on. To load the code only on pages with facets, use thefacetwp_scripts
hook. To load it on all pages, usewp_head
orwp_footer
. Or you can use a code snippets plugin. More infovar response = fetch('https://yoursite.com/wp-json/facetwp/v1/fetch', { 'method': 'POST', 'headers': { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa('username:password') // base64-encoded }, 'body': JSON.stringify(request_data) }) .then(response => response.json()) .then(result => console.log(result));
Response data
{ "results":[ 1, 2, 3, 4 ], "facets":{ "make":{ "name":"make", "label":"Make", "type":"checkboxes", "selected":[ "audi" ], "choices":[ { "value":"audi", "label":"Audi", "depth":0, "count":20 } ] }, "vehicle_type":{ "name":"vehicle_type", "label":"Vehicle Type", "type":"checkboxes", "selected":[ ], "choices":[ { "value":"car", "label":"Car", "depth":0, "count":17 }, { "value":"suv", "label":"SUV", "depth":0, "count":3 }, { "value":"minivan", "label":"Minivan", "depth":0, "count":0 }, { "value":"truck", "label":"Truck", "depth":0, "count":0 }, { "value":"van", "label":"Van", "depth":0, "count":0 } ] } }, "pager":{ "page":1, "per_page":4, "total_rows":20, "total_pages":5 } }
Trigger the indexer programmatically
You may need to trigger the indexer programmatically if posts aren’t saved via the normal /wp-admin/.
The second code sample on the above-linked section shows how to force FacetWP to re-index a specific post.