Created
October 24, 2019 02:56
-
-
Save claygriffiths/bdd06373bb04ab7a62fd0330c23d619d to your computer and use it in GitHub Desktop.
GP Populate Anything: Filter by relative dates
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 | |
/** | |
* 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; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{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.