Skip to content

Instantly share code, notes, and snippets.

@claygriffiths
Created October 24, 2019 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save claygriffiths/bdd06373bb04ab7a62fd0330c23d619d to your computer and use it in GitHub Desktop.
Save claygriffiths/bdd06373bb04ab7a62fd0330c23d619d to your computer and use it in GitHub Desktop.
GP Populate Anything: Filter by relative dates
<?php
/**
* 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}
**/
add_filter( 'gppa_replace_filter_value_variables_post', function ( $value ) {
preg_match_all( '/{(\d+) ((week|day|month|year)s?) ago}/m', $value, $matches, PREG_SET_ORDER, 0 );
if ( ! $matches || ! count( $matches ) ) {
return $value;
}
foreach ( $matches as $match ) {
$value = str_replace( $match[0], date( 'Y-m-d H:i:s', strtotime( '-' . $match[1] . ' ' . $match[2] ) ), $value );
}
return $value;
} );
@transitriders
Copy link

{X hours ago} is not working with this snippet. {X days ago} works as it should. Using latest GF, Perks, and Populate Anything plugins on a fresh Wordpress install.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment