Created
October 11, 2023 11:57
-
-
Save frikishaan/6e76ae1fd1693cfea4a31307b66e6775 to your computer and use it in GitHub Desktop.
Filter posts by meta value - Generate Press
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* In this example, we are filtering the custom post type named 'Events' | |
*/ | |
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { | |
// apply filter if loop has class: upcoming-events-grid | |
if (! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'upcoming-events-grid' ) !== false) { | |
$query_args = array_merge( $query_args, array( | |
'meta_key' => 'start_date', | |
'meta_type' => 'DATE', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
'meta_query' => array( | |
array( | |
'key' => 'start_date', // Check the start date field | |
'value' => date("Y-m-d"), // Set today's date (note the similar format) | |
'compare' => '>=', // Return the ones greater than today's date | |
'type' => 'DATE' // Let WordPress know we're working with date | |
) | |
), | |
)); | |
} | |
return $query_args; | |
}, 10, 2 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment