-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-include-exclude.php
More file actions
43 lines (38 loc) · 955 Bytes
/
query-include-exclude.php
File metadata and controls
43 lines (38 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* Plugin Name: Query Include Exclude
* Description: Adds the ability to specify which posts to include or exclude in a query loop.
* Version: 1.0.0
* Author: BoxUK
* Author URI: https://boxuk.com
* Requires at least: 6.4
*
* @package Boxuk\QueryIncludeExclude
*/
declare( strict_types=1 );
namespace Boxuk\QueryIncludeExclude;
add_action(
'enqueue_block_editor_assets',
function () {
$asset = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
wp_enqueue_script(
'boxuk-query-include-exclude',
plugins_url( 'build/index.js', __FILE__ ),
$asset['dependencies'],
$asset['version'],
true
);
}
);
add_filter(
'query_loop_block_query_vars',
function ( array $query, \WP_Block $block ): array {
$block_query = $block->context['query'] ?? [];
if ( ! empty( $block_query['include'] ) ) {
$query['post__in'] = array_map( 'intval', $block_query['include'] );
}
return $query;
},
10,
2
);