Sauter la navigation

[Résolu] How to filter about age from date custom field

This support ticket is created Il y a 5 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 6 réponses, a 2 voix.

Dernière mise à jour par Nigel Il y a 5 années et 10 mois.

Assisté par: Nigel.

Auteur
Publications
#1219268

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:

add_shortcode( 'iam-eta-mio-profilo', 'wpv_post_age_shortcode_b');
function wpv_post_age_shortcode_b( $atts ) {
 $a = shortcode_atts( array(
        'postid' => 0,
    ), $atts );
 $timestamp = get_post_meta( $a['postid'], 'wpcf-data-di-nascita', true );
$age = floor((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}

Now I need to know how to create a Query Filter to not show the Profile Posts that do not meet the age condition.

Is there any documentation that you are following? No, I think no.

Is there a similar example that we can see? No...

What is the link to your site? lien caché

#1219496

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Screenshot 2019-03-22 at 09.11.41.png

Hi Francesco

Your shortcode isn't quite right, because it doesn't take account of leap years.

So, here is an example of a shortcode used to calculate the age from a date field which should calculate the age correctly: https://toolset.com/forums/topic/calculating-dates/#post-1114234

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:

[wpv-view name="adults" before="[eighteen]"]
#1220863

Hi Nigel,
thanks for your reply.
I will try soon your code to calculate the age: https://toolset.com/forums/topic/calculating-dates/#post-1114234.

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?

#1220913
Mio Profilo incontrAmici shortcode eta.png
Mio Profilo incontrAmici shortcode eta mio profilo.png

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)

#1220977

Nigel
Supporter

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":

[wpv-view name="adults" before="[eighteen]"]

Does that make sense?

#1220983

1 - The code snippets were activated, but return only the shortcode strings... I don't know why...

2 - Ok, that does make sense. But, is it necessary to use the attribute in the View shortcode? Can I use only the Query Filter?
I will try it soon.

#1221025

Nigel
Supporter

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.