How can I filter posts, to display the posts for the current year only
I have a cpt with many posts, but only want to show posts published in 2023…
as the build-in filter can only select taxonomies…
How can I filter posts, to display the posts for the current year only
I have a cpt with many posts, but only want to show posts published in 2023…
as the build-in filter can only select taxonomies…
Hey Casper,
you can use the bricks/posts/query_vars hook:
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id !== 'kdmsem' ) {
return $query_vars;
}
$query_vars['year'] = wp_date( 'Y' );
return $query_vars;
}, 10, 3 );
Make sure to replace the kdmsem
in my snippet with your Posts element’s ID.
Best,
André
Thank you for the assist, Andre
Hi Andre, I don’t know if my question is directly related with the solution you posted but I think it is somehow related to what I am trying to achieve.
I have a custom post type called News (created with ACF) and I am displaying all of the news posts in an archive template in which I am using the native Bricks filters for filtering by topic and other taxonomies. So far, so good.
I want to also add a select filter that will query the year of the news posts and let me select and filter by the published year (2021, 2022, 2023, etc.).
Unfortunately, as of now, Bricks “select” filter can only query Post ID, Post type, Post status and Post author.
Can your above PHP be implemented in this scenario or is it a completely different thing?
Thanks!