Skip Navigation

[Resolved] Get next 3 upcoming birthday

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

Problem:
The issue here is that the user had a birthday field but wanted to get the next 3 posts with their upcoming birthdays.
Solution:

The customer was able to resolve the issue with the query below.

SELECT user_id,FROM_UNIXTIME(`meta_value`) as bdate
FROM  wp_usermeta 
WHERE `meta_key` = 'wpcf-geboortedatum' AND DATE_ADD(FROM_UNIXTIME(`meta_value`), 
                INTERVAL YEAR(CURDATE())-YEAR(FROM_UNIXTIME(`meta_value`))
                         + IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(FROM_UNIXTIME(`meta_value`)),1,0)
                YEAR)
            BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 YEAR) AND (MONTH(FROM_UNIXTIME(`meta_value`)) <> MONTH(CURDATE()) OR DAY(FROM_UNIXTIME(`meta_value`)) > DAY(CURDATE())) order by IF(MONTH(bdate) < MONTH(NOW()), MONTH(bdate) + 12, MONTH(bdate)),
 DAY(bdate) limit 3

So essentially a custom database query is required for this

This support ticket is created 7 years 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 2 voices.

Last updated by marcB-6 7 years ago.

Assisted by: Shane.

Author
Posts
#598433

I would like to get next 3 upcoming birthdays. I have tried the smae thing done here : https://toolset.com/forums/topic/upcoming-birthdays/

Using toolset views filter it is not giving me proper result.

#598666

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Marc,

Thank you for contacting our support forum.

If I understand correctly then you just want to return posts for the next 3 dates essentially since the next 3 dates would be birthdays.

Why not set your view to sort by the custom date field and then add a date filter for that birthday field and set it to list the days that are greater than today.

Finally you can adjust the limit to 3 so essentially your view will sort by the next days and only bring up posts that are 3 days in the future from today.

Please let me know if this provides any insight on this .

Thanks,
Shane

#598757

Hi Shane,

I have already followed the steps you mentioned above before generating support ticket.
Problem is, it is giving the birthdays which are grater than today respective of year.
So it is giving me users which are having birthday in current year and more than that year.
for example,
I have a Person with the birthday 12/14/1980
Today is 12/14/2017
If I set a filter by that date field and set the condition to the date to be after today like you said...it would never return that Person because the birthday is in the past and not after today (1980 is in the past)...

#599000

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Marc,

I see the issue clearer now. I'll perform some testing on this and get back to you as soon as possible.

Thanks,
Shane

#599407

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Marc,

The only way I can think of this is to exclude the year from the query.

This means we would need to perform a manual query using the wpv-filter-vew hook to do this instead of the GUI.

We should also be able to perform the query by specifically asking for a date.

Please let me know if this helps.

Also I could write up the idea that I have in mind if you'd like.

Thanks,
Shane

#599696

Also I could write up the idea that I have in mind if you'd like.
Ans : Yes, Please provide me with working code.

#600276

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mark,

I'm still working on this, just a bit loaded at the moment with some other tickets.

Just an update i'm looking at this here to get this working
https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

So what I want to do is to get today's date in month and day and then find the next 3 dates in the database after today in month and day.

Hopefully this can provide some clue to get this to work until i'm able to provide the proposed solution i.e if it works.

Thanks,
Shane

#600391

Thanks Shane for the Article..
Note : Date is stored in database as timestamp..

#600854

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Marc,

I was brainstorming and came up with an idea because i was running into some issues creating the filter.

Why not store each entity of the field in a separate custom field.

This way we can evaluate the day and month without having to worry about removing the year. So instead of having a datepicker we can create our own by create separate select fields for Month Day and Year where year can just be a simple numeric field.

So from this its still going to be difficult to get the next 3 birthdays but atleast its easier to query.

Please let me know what you think about this.

Thanks,
Shane

#601014

Thank You So Much Shane!!
I did it using below Query..It will help others.

SELECT user_id,FROM_UNIXTIME(`meta_value`) as bdate
FROM  wp_usermeta 
WHERE `meta_key` = 'wpcf-geboortedatum' AND DATE_ADD(FROM_UNIXTIME(`meta_value`), 
                INTERVAL YEAR(CURDATE())-YEAR(FROM_UNIXTIME(`meta_value`))
                         + IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(FROM_UNIXTIME(`meta_value`)),1,0)
                YEAR)
            BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 YEAR) AND (MONTH(FROM_UNIXTIME(`meta_value`)) <> MONTH(CURDATE()) OR DAY(FROM_UNIXTIME(`meta_value`)) > DAY(CURDATE())) order by IF(MONTH(bdate) < MONTH(NOW()), MONTH(bdate) + 12, MONTH(bdate)),
 DAY(bdate) limit 3

I have created shortcode to display it and deleted the view.

Thanks for your support!!