Last active
February 14, 2020 18:57
-
-
Save carlosonweb/a25abcc023f462521330cda36e14debe to your computer and use it in GitHub Desktop.
Beaver Builder Filter Hook: fl_builder_loop_query_args To Filter by Meta Keys
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
/** | |
* Add this to the child theme's functions.php file. | |
*/ | |
add_filter( 'fl_builder_loop_query_args', function( $query_args ){ | |
$today = date( 'd.m.Y', time() ); | |
// Past ( today > start_date ) | |
if ( 'PAST_POST_MODULE_ID_HERE' == $query_args[ 'settings' ]->id ) { | |
$query_args[ 'meta_query' ] = array( | |
'key' => 'end_date', | |
'value' => $today, | |
'type' => 'date', | |
'compare' => '<' | |
); | |
} | |
// Future ( today < start_date ) | |
if ( 'FUTURE_POST_MODULE_ID_HERE' == $query_args[ 'settings' ]->id ) { | |
$query_args[ 'meta_query' ] = array( | |
'key' => 'start_date', | |
'value' => $today, | |
'type' => 'date', | |
'compare' => '>' | |
); | |
} | |
// Current ( start_date <= today <= end_date ) | |
if ( 'CURRENT_POST_MODULE_ID_HERE' == $query_args[ 'settings' ]->id ) { | |
$query_args[ 'meta_query' ] = array( | |
'relation' => 'AND', | |
array( | |
'key' => 'start_date', | |
'value' => $today, | |
'type' => 'date', | |
'compare'=> '>=' | |
), | |
array( | |
'key' => 'end_date', | |
'value' => $today, | |
'type' => 'date', | |
'compare' => '<=' | |
) | |
) | |
} | |
return $query_args; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment