WordPress Core Query Block - markhowellsmead/helpers GitHub Wiki
Modify query parameters
Example: post meta end / expiry date. Don't forget to return
the $query
array.
add_filter('query_loop_block_query_vars', [$this, 'excludeExpiredPostsFromQueryBlock']);
…
public function excludeExpiredPostsFromQueryBlock($query)
{
if ($query['post_type'] ?? '' === 'post') {
$this->applyEnddateMetaQuery($query);
}
return $query;
}
// Function to apply the 'enddate' and 'enddateactive' meta query logic
public function applyEnddateMetaQuery(&$query)
{
$current_time = current_time('Y-m-d H:i:s');
$meta_query = [
'relation' => 'OR',
[
'relation' => 'OR',
[
'key' => 'enddateactive',
'compare' => 'NOT EXISTS',
],
[
'key' => 'enddateactive',
'value' => 'false',
'compare' => '=',
'type' => 'BOOLEAN',
],
],
[
'relation' => 'OR',
[
'key' => 'enddate',
'compare' => 'NOT EXISTS',
],
[
'key' => 'enddate',
'value' => $current_time,
'compare' => '>=',
'type' => 'DATETIME',
],
],
];
if (is_array($query)) {
$query['meta_query'][] = $meta_query;
} else {
// For normal WP queries, use set method
$query->set('meta_query', $meta_query);
}
}