Skip Navigation

[Resolved] Set up a View that list date field birthday

This thread is resolved. Here is a description of the problem and solution.

Problem:
Set up a View that list date field birthday today - list all posts who has birthday today

Solution:
To display only posts to compare the custom date field which holds the birthdate and compare the birthdate with today's date, you need to add the following filter to your current theme's functions.php file.

add_filter( 'posts_where' , 'func_filter_birtdate_posts');
function func_filter_birtdate_posts( $where ) {
    global $WP_Views;
    global $wpdb;
  
    if($WP_Views->current_view == 9999){
          
           $where .= " And ".$wpdb->prefix."posts.ID IN  (SELECT post_id FROM ".$wpdb->prefix."postmeta
                                                WHERE (".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID)
                                                AND ".$wpdb->prefix."postmeta.meta_key = 'wpcf-data-di-nascita'
                                                AND month(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) = month(now())
                          AND day(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) = day(now())
                                                 
                                            )";
 
           
   
    }
    return $where;
}

Where:
Replace 9999 with your original view ID

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/set-up-a-view-that-list-date-field-birthday/#post-1263805

Relevant Documentation:

This support ticket is created 4 years, 10 months ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by culturaI 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1263479

Hello support!

I have:
CPT: contatti
CUSTOM FIELD DATE (to use for the date of birth ): data di nascita

I would to set up a View that list the people (contatti) who make the years today or, even better in a range of dates chosen.

Not sure about the better and simplest way to proceed.

Thanks

#1263645

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - do you mean that you want to list the posts that belong to post type "contatti" who has a birthday today by comparing the custom date field "data di nascita" value?

#1263771

Hello Minesh and thanks for your help.

Yes, i have to list the posts that belong to post type "contatti" who has a birthday today by comparing the custom date field "data di nascita" value.

#1263805

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - to display only posts to compare the custom date field which holds the birthdate and compare the birthdate with today's date, you need to add the following filter to your current theme's functions.php file.

add_filter( 'posts_where' , 'func_filter_birtdate_posts');
function func_filter_birtdate_posts( $where ) {
    global $WP_Views;
    global $wpdb;
 
    if($WP_Views->current_view == 9999){
         
           $where .= " And ".$wpdb->prefix."posts.ID IN  (SELECT post_id FROM ".$wpdb->prefix."postmeta
                                                WHERE (".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID)
                                                AND ".$wpdb->prefix."postmeta.meta_key = 'wpcf-data-di-nascita'
                                                AND month(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) = month(now())
					      AND day(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) = day(now())
												
                                            )";

		  
  
    }
    return $where;
}

Where:
Replace 9999 with your original view ID

#1264485

It's working simply perfectly.

Thanks Minesh!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.