Download the PHP package aucor/wp_query-route-to-rest-api without Composer
On this page you can find all versions of the php package aucor/wp_query-route-to-rest-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aucor/wp_query-route-to-rest-api
More information about aucor/wp_query-route-to-rest-api
Files in aucor/wp_query-route-to-rest-api
Package wp_query-route-to-rest-api
Short Description Adds new route /wp-json/wp_query/args/ to REST API
License GPL-2.0-or-later
Homepage https://github.com/aucor/wp_query-route-to-rest-api
Informations about the package wp_query-route-to-rest-api
WP_Query Route To REST API
Contributors: Teemu Suoranta, Sami Keijonen, Christian Nikkanen
Tags: WordPress, REST API, WP_Query
License: GPLv2+
- Description
- How to use
- Basic usage
- Use with PHP
- Use with JS
- Advanced examples
- Advanced example: tax_query
- Advanced example: tax_query with relation
- Advanced example: modifying existing WP_Query (post archive, term archive, search etc)
- Restrictions
- Allowed args
- Post types
- Post status
- Restriction fail-safe
- Default WP_Query
- Extra plugin compatibility features
- Filters
- Hooks
- Install
- Issues and feature whishlist
- Changelog
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1
Description
Adds new route /wp-json/wp_query/args/
to REST API. You can query content with WP_Query args. There's extensive filters and actions to limit or extend functionality.
How to use
Basic usage
Route: /wp-json/wp_query/args/
Get three projects: /wp-json/wp_query/args/?post_type=project&posts_per_page=3
You shoudn't write query args by hand! It gets very complicated when you want to pass arrays for example with meta_query.
Use with PHP
1. Create $args
2. Turn $args into query string (Reference)
3. Make the call
Use with JS
1. Create args
2 a) Create params with for example using @wordpress/url
package
2 b) Some other JS solution query-string handles most use cases, but as query strings aren't really standardized, YMMV.
One example of where it falls short:
One possible solution, ES2015:
2 c) Create params with jQuery
3. Make the call
Or with jQuery.
Advanced examples
Advanced example: tax_query
Get posts that have both tags "wordpress" and "woocommerce"
PHP:
JS:
Advanced example: tax_query with relation
Get posts that have either "wordpress" or "woocommerce" tag. This gets tricky because JS doesn't support completely the same array structure as PHP. If you only need PHP, this is a piece of cake.
PHP:
JS:
For other uses, keep in mind JS object/array syntax. If there's key + value, use object {}
. If theres only value, use array []
.
Advanced example: modifying existing WP_Query (post archive, term archive, search etc)
Sometimes you need to create features that add small tweaks to current query that WordPress, theme or plugins has already defined. These include "load more" buttons, filters etc. You can create that query from scratch if you want, but there is a neat way to get the current query for JS.
You can add this to your archive.php
or whatever PHP template you need:
Now you can access the query in JS from this var wp_query
. Props @timiwahalahti for this idea.
Restrictions
The route /wp-json/wp_query/args/
sets some restrictions by default for queries. These restrictions can be lifted or hardened with filters and actions.
Allowed args
So biggest ones missing have something to do with getting content that you might not want to get like post_status
drafts (add this argument to the list with filter if you need it). By default, no querying post_passwords
or having your way with cache settings.
Post types
By default all the post types marked 'show_in_rest' => true
are available. 'post_type' => 'any'
falls back to these post types. You can change post types with filter to what you want.
Post status
By default, only "publish" is allowed. Add other post_status as needed with filter.
Restriction fail-safe
Addition to restriction of WP_Query args, there is check after the query that queried posts will not be forbidden post types or post_status.
Default WP_Query
In addition to the normal defaults from WP_Query.
Extra plugin compatibility features
This plugin has built-in compatibility for Relevanssi ('s' argument) and Polylang ('lang' argument)
Filters
Add more allowed args:
Add more default args:
Add allowed post types:
You can also add post types by setting 'show_in_rest' => true
when registering post type.
Add allowed post status:
Is current post allowed:
Alter any argument value:
Check permissions:
Limit max posts per page:
Modify default $data:
Modify $data after loop:
Remove post type meta:
Remove parent class:
Hooks
Before WP_Query:
After WP_Query:
Install
Download and activate. That's it.
Composer:
With composer.json:
Issues and feature whishlist
This is a WordPress plugin by 3rd party developer. WordPress.org or Automattic has nothing to do with this plugin. There's no warranty or quarantees. Thread carefully.
If you see a critical functionality missing, please contribute!
Looking for similar API to WP_User_Query?
Install also MEOM/meom-user-query
Changelog
1.3.2
Fix PHP warning caused by 1.3.0 argument sanitizing refactoring.
1.3.1
Fix composer license to a valid license.
1.3.0
Compatibility release with a few new features. 100% backwards compatible.
- Custom HTML output is now allowed (#10)
- Instance and argument sanitizing can be now reused (#11)
- Typos in filter names are fixed while keeping old names also working (#5)
- Updates WP version, plugin version and some readme tweaks
1.2.0
WordPress.org release.
1.1.1
Added advanced example in readme for getting PHP WP_Query for JS. Added table of contents. Made the title hierarchy more logical.
1.1
Make the return data structure same as /wp-json/wp/posts/. The data schema was missing some data before. Now the structure is inherited from the WP_REST_Posts_Controller as it should have from the start.