wpqueryjs
v1.1.1
Published
An axios wrapper for the WordPress REST API.
Downloads
1
Readme
WPQuery.js
An axios wrapper for the WordPress REST API.
Status
Getting Started
- Download the latest release.
- Clone the repo:
git clone https://github.com/juicymedialtd/WPQuery.js.git
- Install with npm:
npm install wpqueryjs
Methods
request(method, resource, params, config)
A method that returns a Promise with the data that you have queried for. All you have to do is pass the HTTP request you want to perform, along with a resource e.g. Tags, Posts or a Custom Post Type and the parameters you wish to include in your query.
If you want to include any global parameters in your request, you don't need to include the _
character as this will be done for you. Also, when requesting a single listing of a resource you can do this by adding a id
property to the params
object like the examples below.
Any camel case properties e.g. perPage
passed in the params
object will change to lowercase and an _
will be added where appropriate. e.g. perPage
will change to per_page
in your query.
Retrieves a page.
const query = new WPQuery('https://demo.wp-api.org')
.request('get', 'pages', {
id: 2,
});
Retrieves the latest 3 posts published in multiple tags.
const query = new WPQuery('https://demo.wp-api.org')
.request('get', 'posts', {
perPage: 3,
tags: [6, 4],
});
Retrieves 2 users in descending order by their IDs.
const query = new WPQuery('https://demo.wp-api.org')
.request('get', 'users', {
order: 'desc',
orderby: 'id',
perPage: 2,
});
Check the WordPress REST API Handbook for all available resources and parameters.
get(resource, params, config)
A helper for the request
method which can be used like the examples below:
Retrieves posts.
const query = new WPQuery('https://demo.wp-api.org').get();
Retrieves posts published by a specific author.
const query = new WPQuery('https://demo.wp-api.org')
.get('posts', {
author: 103,
});
Retrieves posts published in a specific category.
const query = new WPQuery('https://demo.wp-api.org')
.get('posts', {
category: 11,
});
Check the WordPress REST API Handbook for all available resources and parameters.
post(resource, params, config)
Note: Authorization will be required for all POST requests to your WordPress site, see Authorization for more information.
A helper method for the request
method which can be used like the examples below if you are authorizing your requests with a nonce:
Creates a new post.
const query = new WPQuery('https://demo.wp-api.org')
.post('posts', {
title: 'The title of my new post',
content: 'The content of my new post.',
}, {
headers: {
'X-WP-NONCE': '',
}
});
Creates a new category.
const query = new WPQuery('https://demo.wp-api.org')
.post('categories', {
name: 'Music'
}, {
headers: {
'X-WP-NONCE': '',
}
});
Creates a new user.
const query = new WPQuery('https://demo.wp-api.org')
.post('users', {
username: 'johnsmith',
email: '[email protected]',
password: '4QKu$jq6?+Gu,uVs,9oG',
}, {
headers: {
'X-WP-NONCE': '',
}
});
Check the WordPress REST API Handbook for all available resources and parameters.
Compatibility
WPQuery.js supports the latest, stable releases of all major browsers except Internet Explorer.
We use JavaScript promises as they offer superior advantages than event listeners. You can find browser compatibility statistics for Promises on Can I use....
However, for a quick overview - read the table below which shows you the browser support for WPQuery.js. All browser tests are conducted with Browser Stack.
| Microsoft Edge | Mozilla Firefox | Google Chrome | Safari | Opera | | --------- | --------- | --------- | --------- | --------- | | >=12 | >=30 | >=32 | >=7.1 | >=20 |
Contributing
Feel free to submit a pull request with any changes that will help make this project better!