Download the PHP package boxybird/boxybird-wp-query-endpoint without Composer
On this page you can find all versions of the php package boxybird/boxybird-wp-query-endpoint. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package boxybird-wp-query-endpoint
WP_Query as a REST API Endpoint
Query anything you want from the WordPress database using a single REST API endpoint.
Simply put, this plugin allows to pass GET params from a url into the WP_Query
class as $args
WP_Query
Reference: https://developer.wordpress.org/reference/classes/wp_query/
Installation
Clone or Download plugin and run composer install
before activating in WordPress Admin.
Usage Examples
PHP
Normally in a WordPress theme or plugin you would create an array of $args and pass them into the WP_Query($args)
constructor. Like this:
Using this plugin, you can pass those same args from your front-end Javascript:
Vanilla JS
jQuery
Hooks
The examples below will use a hypothetical site with a 'movie' post_type.
Formatting the JSON response
Out of the box, WP_Query
will return the raw rows from the wp_posts
table. Like this:
The above may be useful in some situations, but more often than not you'll likely want to format the JSON response. This is the filter to do it:
Here's an example of how you could use the filter:
Focusing on the 'movie' post_type above, this would be the custom formatted response:
Default/Overriding WP_Query Args
Default Args
If you would like to add default WP_Query $args
BEFORE the request params are applied, this is the filter to do it.
Note: The above are defaults only. Meaning, if the incoming request specifies
posts_per_page
, it will override theboxybird/query/default-args
filter defaults.
Override Args
If you would like to modify/remove incoming request params BEFORE running the WP_Query
, this is the filter to do it.
Note: The above filter can be thought of as a security layer. If you never want an
$arg
to be passed toWP_Query
, do it here!
Permissions Callback
If you would like to protected who has access to the /wp-json/boxybird/wp-query/v1/args
endpoint, this is the filter.