Updated on 1 Feb 2024
This Pro tutorial shows how to limit the posts output by either the Posts element or a Query Loop in Bricks to those published in the last 14 days.
Query Loop
Enable Query editor (PHP) and paste:
$date_query = [
[
'after' => '14 days ago',
]
];
return [
'posts_per_page' => 100, // a large number
'date_query' => $date_query,
];
Change 100 to a number that is typically higher than the possible number of matches.
Posts element
bricks/posts/query_vars filter to the rescue! (this also works with query loops)
Add this in child theme‘s functions.php or a code snippets plugin:
// Prefilter posts to those published in the last 14 days.
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
$date_query = [
[
'after' => '14 days ago',
]
];
if ( 'gcgexo' === $element_id ) {
$query_vars['date_query'] = $date_query;
}
return $query_vars;
}, 10, 3 );
Replace gcgexo with the Bricks ID of your Posts element.
Reference
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters