An acorn package providing a "Query Block" for the Gutenberg editor.
This package is a wordpress-package: it is installed into the site's webroot
rather than into vendor/, so that the editor assets it ships can be served by
the web server.
-
Allow the Composer plugin that places it there:
composer config allow-plugins.yard/wp-package-installer true -
Set the install directory (Bedrock / Sage projects):
composer config extra.wordpress-packages-install-dir web/app/packages
-
Ignore the packages directory:
echo "web/app/packages" >> .gitignore
-
Install this package with Composer:
composer require yard/query-block
-
Run the Acorn WP-CLI command to discover this package:
wp acorn package:discover
Before v3 the editor assets were served through an Acorn route, whose URL was
built from app.url — one fixed host for a whole multisite network. Subsites
therefore loaded the assets from another site's domain, which WordPress 7.1
blocks in the admin. v3 drops the route and resolves the assets from the
webroot of the site being edited.
Apply steps 1 to 3 above, then:
composer require yard/query-block:^3.0 -WWhen this package is installed, you can insert the “Berichtenlijst” block in the Gutenberg editor. This block lists a series of posts based on different filter settings in the admin.
A default template is included in the package. You can publish the template to your project with:
wp acorn vendor:publish --provider="Yard\QueryBlock\QueryBlockServiceProvider"This will copy the view default.php from this package into your project at /sage/resources/views/vendor/yard-query-block/templates/default.blade.php. You can now modify the default template as desired.
You can create additional templates by placing a template file in the same directory. For example:
/sage/resources/views/vendor/yard-query-block/templates/horizontal.blade.php
Add the template name as a comment at the top of this template file like this:
@php
/**
* Template: Horizontal
*
* @var Illuminate\Support\Collection|Yard\Data\PostData $postDataCollection
* @var Yard\QueryBlock\Block\BlockAttributes $attributes
*/
@endphpNow, you will be able to select the template from the editor. The name of the template is displayed using the value in your docblock.
It is possible to add a connection filter to the block settings that lets you filter the available posts based on connected posts. This only works for the ACF "Post Object" field.
Important: For this to work, the Post Object field setting "Select Multiple" must be disabled.
All you need to do is publish config/yard-query-block.php and register all connections in the connections array. For example:
return [
'connections' => [
[
'from' => 'news',
'to' => 'project',
'meta_key' => 'news_related_project',
],
],
];Customize which controls are displayed in the block's inspector panel.
import { addFilter } from '@wordpress/hooks';
addFilter(
'yard.query-inspector-config',
'yard.query-inspector-config',
( config, attributes ) => {
return {
...config,
showPostTypeSelectControl: false,
showNumberOfPostsRangeControl: false,
};
}
);Exclude specific post types from the list of available post types.
import { addFilter } from '@wordpress/hooks';
addFilter(
'yard.query-exclude-post-types',
'yard.query-exclude-post-types',
( excludedPostTypes ) => {
return [
...excludedPostTypes,
'healthcare-provider',
'location',
'page',
];
}
);Exclude specific taxonomies from the list of available taxonomies.
import { addFilter } from '@wordpress/hooks';
addFilter(
'yard.query-exclude-taxonomies',
'yard.query-exclude-taxonomies',
( excludedTaxonomies ) => {
return [ 'category', 'post_tag' ];
}
);Customize the minimum and maximum value for the posts per page range.
import { addFilter } from '@wordpress/hooks';
addFilter(
'yard.query-max-number-of-posts',
'yard.query-max-number-of-posts',
(defaultMax, attributes) => {
const postTypeValues = attributes.postTypes?.map((type) => type.value) || [];
if (postTypeValues.includes('news')) {
return 5;
}
if (postTypeValues.includes('healthcare-provider')) {
return 2;
}
return defaultMax;
}
);Change the post type select control from multi to single select.
import { addFilter } from '@wordpress/hooks';
addFilter(
'yard.query-post-type-select-control-is-multi',
'yard.query-post-type-select-control-is-multi',
() => false
);Filters the Post Query before it is executed on the database.
| Parameters | Type | Description |
|---|---|---|
| $query | \Corcel\Model\Builder\PostBuilder | The query object |
| $attributes | \Yard\QueryBlock\Block\BlockAttributes | The block attributes |
| Return | Type | Description |
|---|---|---|
| $query | \Corcel\Model\Builder\PostBuilder | The query object |
Example:
add_filter('yard_query_block_post_query', function ($query, $attributes) {
if (is_user_logged_in()) {
return $query;
}
return $query->hasMeta('post_is_public', 'yes');
}, 10, 2);