PHP code example of boxybird / boxybird-wp-query-endpoint
1. Go to this page and download the library: Download boxybird/boxybird-wp-query-endpoint library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
boxybird / boxybird-wp-query-endpoint example snippets
$args = [
'post_type' => 'post',
'orderby' => 'title',
'posts_per_page' => 12,
'category__in' => [31, 12, 4],
//... any other WP_Query args you want.
];
$query = new WP_Query($args);
// loop through results in PHP file.
add_filter('boxybird/query/format-response', function (WP_Query $query) {
// do something with $query and return.
});
// Example
add_filter('boxybird/query/default-args', function ($args) {
$args['posts_per_page'] = 12;
return $args;
});
// Example.
add_filter('boxybird/query/override-args', function ($args) {
// Don't allow more than 20 'posts_per_page'.
if (isset($args['posts_per_page']) && $args['posts_per_page'] > 20) {
$args['posts_per_page'] = 20;
}
// Always override 'post_status'
$args['post_status'] = 'publish';
return $args;
});
// Basic Example.
add_filter('boxybird/query/permission', function () {
// Only logged in users have access.
return is_user_logged_in();
});
// Example taken from the WordPress docs.
add_filter('boxybird/query/permission', function () {
// Restrict endpoint to only users who have the edit_posts capability.
if (!current_user_can('edit_posts')) {
return new WP_Error('rest_forbidden', esc_html__('OMG you can not view private data.', 'my-text-domain'), ['status' => 401]);
}
// This is a black-listing approach. You could alternatively do this via white-listing, by returning false here and changing the permissions check.
return true;
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.