Tell us what you are trying to do?
So, in all pages where are displayed the lists of CPT profile posts in a dating site, I need to display only the CPT Profile posts that have the age >= 18 years (or NOT display the CPT Profile posts that have the age < 18 years).
So, for the age calculation, I have created a Date Custom Field, and after I have created this shortcode to display the age in the CPT Profile Posts:
Now, when it comes to filtering a View to only show profile posts where checking the date of birth shows them to be eighteen years old, you will want to register a different shortcode, one which returns the timestamp of 18 years ago from today.
Like this, for example:
/**
* Register shortcode to return timestamp from 18 years ago
*/
add_shortcode('eighteen', function () {
$date = new DateTime();
$date->modify('-18 years');
return $date->format('U');
});
Now add a Query Filter to your View to compare the date of birth field (which is saved as a timestamp) with the timestamp from 18 years ago, which you will pass as a shortcode attribute. See screenshot.
I've chosen "before" as the name of the attribute.
You'll need to register the 'eighteen' shortcode at Toolset > Settings > Front-end Content to be able to use it as an attribute.
Then when inserting your View add the attribute, something like:
So, why I need to create this new shortcode which returns the timestamp of 18 years ago from today? And what should I do to compare the timestamp of 18 years ago from today with the birth date? The attribute before do that? If yes, where is the code that do that? And the attribute before where is set as attribute?
And If I want to show an alternative content to the users that have age < 18, where can I do that?
So,
I have changed my shortcode content with the content in your shortcode (https://toolset.com/forums/topic/calculating-dates/#post-1114234)
I have 2 shortcode to display the age, one with $post->ID, and one other with attribute $a['postid']
the first code is:
/**
* Calculate age (in years) from date field
* specified as shortcode attribute 'slug'
*/
add_shortcode( 'iam-eta', function( $atts = [], $content = null ){
// provide defaults
$atts = shortcode_atts(
array(
'slug' => null
),
$atts
);
$age = '';
if ( isset( $atts['slug'] ) ) {
global $post;
$timestamp = get_post_meta( $post->ID, 'wpcf-data-di-nascita' . $atts['slug'], true );
$date = DateTime::createFromFormat( "U", $timestamp );
$now = new DateTime();
$diff = $now->diff( $date );
$age = $diff->y;
}
return $age;
});
the second code is:
/**
* Calculate age (in years) from date field
* specified as shortcode attribute 'slug'
*/
add_shortcode( 'iam-eta-mio-profilo', function( $atts = [], $content = null ){
// provide defaults
$atts = shortcode_atts(
array(
'slug' => null
),
$atts
);
$age = '';
if ( isset( $atts['slug'] ) ) {
global $post;
$a = shortcode_atts( array(
'postid' => 0,
), $atts );
$timestamp = get_post_meta( $a['postid'], 'wpcf-data-di-nascita' . $atts['slug'], true );
$date = DateTime::createFromFormat( "U", $timestamp );
$now = new DateTime();
$diff = $now->diff( $date );
$age = $diff->y;
}
return $age;
});
But in the template, they display only the shortcode string... (see the attached images)
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+00:00)
It looks like you didn't activate the code snippets, so the shortcodes are not recognised as shortcodes, they are treated as normal strings.
Regarding why I wrote the shortcode 'eighteen', my understanding was that you have a date-of-birth custom field, and that you want your View to include a filter so that only profile posts for those over 18 years old are returned.
The date-of-birth custom field will be stored as a timestamp (a number, the number of seconds since 1 Jan 1970).
The timestamp from 18 years ago today (26 March 2001) is 985564800.
So your Query Filter should be to include posts with a date-of-birth < 985564800, meaning they were born more than 18 years ago.
The custom shortcode generates the timestamp of 18 years ago, which you pass to the View via a shortcode argument, which in my example I did with an argument called "before":
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+00:00)
1. Where exactly are you inserting the shortcodes? Try inserting them directly on any old test page, without using a page builder or anything like that. Do they work? If not, maybe where you added the code snippets they have an error and so are not initialised?
2. You need to provide the value (e.g. 985564800) to the Query Filter. You can't hard code it. Someone who is not 18 today might be 18 next week.