Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GP Populate Anything: Filter by relative dates
/**
* See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install snippets like these.
*
* The following snippet will limit a specific form to only allow submissions by users with existing submissions.
* -------
* Instructions:
* -------
* 1. Replace 'post' with whatever object type you wish to add these merge tags to.
*
* Note, newer versions of GP Populate Anything support 'gppa_replace_filter_value_variables' which works for all
* object types.
*
* 2. Change the filter value to "Add Custom Value" and insert a merge tag that follows
* the following format: {INTEGER UNIT ago}
*
* Examples:
* {3 days ago}
* {2 hours ago}
* {1 month ago}
* {3 weeks ago}
* {1 year ago}
* {today}
**/
add_filter( 'gppa_replace_filter_value_variables_post', function ( $value ) {
// Check for {n units ago} merge tags.
preg_match_all( '/{(\d+) ((second|minute|hour|day|week|month|year)s?) ago}/im', $value, $matches, PREG_SET_ORDER, 0 );
if ( ! empty( $matches ) ) {
foreach ( $matches as $match ) {
$value = str_replace( $match[0], date( 'Y-m-d H:i:s', strtotime( '-' . $match[1] . ' ' . $match[2] ) ), $value );
}
}
// Check for {today} merge tag.
preg_match_all( '/{today}/im', $value, $matches, PREG_SET_ORDER, 0 );
if ( ! empty( $matches ) ) {
foreach ( $matches as $match ) {
$value = str_replace( $match[0], date( 'Y-m-d H:i:s' ), $value );
}
}
return $value;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.