-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-loop-future-filter.php
More file actions
116 lines (105 loc) · 2.89 KB
/
query-loop-future-filter.php
File metadata and controls
116 lines (105 loc) · 2.89 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Plugin Name: Query Loop Future Filter
* Description: Adds a relative date filtering options to the core Query Loop Block
* Version: 1.0.1
* Author: Sandy McFadden
* Author URI: https://sandy.mcfadden.blog
* Plugin URI: https://sandy.mcfadden.blog/2025/01/25/query-loop-future-filter-plugin/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: query-loop-future-filter
*
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register block editor assets.
*
* @return void
*/
function register_query_block_settings() {
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
wp_register_script(
'custom-query-settings',
plugins_url( 'build/index.js', __FILE__ ),
array_merge(
$asset_file['dependencies'],
array(
'wp-blocks',
'wp-components',
'wp-compose',
'wp-hooks',
'wp-block-editor',
'wp-i18n',
)
),
$asset_file['version'],
true
);
wp_enqueue_script( 'custom-query-settings' );
}
add_action( 'enqueue_block_editor_assets', 'register_query_block_settings' );
/**
* Initialize plugin functionality.
*/
function sm_query_loop_init() {
$post_types = get_post_types( array( 'show_in_rest' => true, 'public' => true ) );
foreach ( $post_types as $post_type ) {
add_filter( "rest_{$post_type}_query", 'sm_query_loop_rest_query', 10, 2 );
}
}
add_action( 'init', 'sm_query_loop_init' );
/**
* Modify REST API query for date filtering.
*
* @param array $args Query arguments.
* @param WP_REST_Request $request Request object.
* @return array Modified query arguments.
*/
function sm_query_loop_rest_query( $args, $request ) {
$date_range = $request->get_param( 'dateRange' );
if ( 'future' === $date_range ) {
$args['date_query'] = array(
'after' => current_time('Y-m-d 00:00:00'),
);
} elseif ( 'past' === $date_range ) {
$args['date_query'] = array(
'before' => current_time('Y-m-d 00:00:00'),
);
}
return $args;
}
/**
* Modify block rendering.
*
* @param string|null $pre_render Pre-render content.
* @param array $block Block data.
* @return string|null
*/
function sm_query_loop_pre_render_block( $pre_render, $block ) {
if ( 'core/query' === $block['blockName'] ) {
add_filter(
'query_loop_block_query_vars',
function( $query ) use ( $block ) {
if ( isset( $block['attrs']['query']['dateRange'] ) && $block['attrs']['query']['dateRange'] ) {
$date_range = $block['attrs']['query']['dateRange'];
if ( 'future' === $date_range ) {
$query['date_query'] = array(
'after' => current_time('Y-m-d 00:00:00'),
);
} elseif ( 'past' === $date_range ) {
$query['date_query'] = array(
'before' => current_time('Y-m-d 00:00:00'),
);
}
}
return $query;
}
);
}
return $pre_render;
}
add_filter( 'pre_render_block', 'sm_query_loop_pre_render_block', 10, 2 );