Posts Published in the Last x Days in Bricks

Looking to limit the posts output by either the Posts element or a Query Loop in Bricks to those published in the last 14 days?

bricks/posts/query_vars filter to the rescue!

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 or query loop element.

Reference

https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

Instant access to all 250+ Bricks code tutorials with BricksLabs Pro

Leave the first comment