Skip Navigation

[Resolved] Northernstars

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

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)

This topic contains 6 replies, has 2 voices.

Last updated by Nigel 7 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#459673

I am trying to:
Display birthdays of actors in a view with function below:
function getbirthdate($bdata) {
$now = time();
$today = date('md',$now);
if(strlen($bdata)==4)
{$bday = DateTime::createFromFormat('Y', $bdata);}else if($bdata != null){$bday = date('md', strtotime($bdata));if ( $bday == $today ) {return 1;}else{return 0;}}
else{return 0;}}

Then in View I use:
[wpv-if evaluate="getbirthdate([types field='birth' output='raw'][/types]) = 1"]<div style="display: flex;min-width: 48%;max-width: 48%;flex-direction: row;justify-content: center;border: 1px solid #ccc ;border-radius: 5px; margin:1%; padding:2%;">[wpv-post-body view_template="BDBox"]</div>[/wpv-if]

It is working on(8g ram 4 core 3.0):
hidden link

Still takes so long to load.

Enabled debugs for now so you can see.

How can I shorten this query; I want to limit it but If i Hard limit or pagination limit then I am unable to display anything as post with todays birthday might be anywhere in the array.

I will be waiting for a reply or an advise.

Birthdays needs to be checked per day; so I dont need to reevaluate this view.
Is there any way to cache this daily and display static content?

Thanks in advance...

#459740

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2016-11-23 at 09.20.48.png

I looked at your link and visited the actors page where I found a box for actors born today.

I assume this is the section you are having a problem with.

From your description it appears you have set this up in a very resource intensive way.

Your view returns all actors, and then for each one you use the conditional shortcode and your custom function to test whether it is the actors birthday today, and, if so, to output a template.

Instead you should create a view which only returns actors born today which outputs the template for each of these (although it looks like you want to limit it to 4 results on that page).

I did that in a simple test.

I created an Actor custom post type and added a date custom field "birthdate".

This gets stored as a UNIX timestamp (i.e. a number) in the wp_postmeta table. So when I create a view "Actors born today" I add a query filter for the birthdate custom field with the condition that it is a number equal to TODAY (see screenshot).

I add that view to a page and it returns just actors born today.

#459972

Hi; first of all thank you for your interest and time.

Birthday custom string is a string not a datetime as some actors don't have exact date but a year.
I had to manipulate date from string.

#459973

Also Today is today's day and year; birthdays are coming from date of birth string.

That way I couldn't make it work...

#459991

What else can be done?

Can I create a view and refresh its cache every day at night?

#459992

"I did that in a simple test.

I created an Actor custom post type and added a date custom field "birthdate".

This gets stored as a UNIX timestamp (i.e. a number) in the wp_postmeta table. So when I create a view "Actors born today" I add a query filter for the birthdate custom field with the condition that it is a number equal to TODAY (see screenshot)."

I have an actor,screenwriter etc custom types, they have a string called "birth", that string is filled by editors, sometimes it is: "October 5, 1975", sometimes it is:"Oct 5th" or sometimes it is: "1956".

I couldn't resolve this with toolset TODAY as it returns for example today: 11/23/16, which doesn't match with 11/23/85 or any incorrect date in "birth" custom string creates an error so I had to manipulate birth string with php. I tried using a shortcode which was given here on forums. But that takes my load time up to 100secs; even worse.

This code particle was advised to someone else; I tried to use it but takes longer as I said:

function born_today_func( $atts ) {
    extract(shortcode_atts(array(
        'birthday' => get_post_meta(get_the_ID(), 'wpcf-birth', true),
  
    ), $atts)
           );
  
    if(((date('d', $birthday)) == date('d')) && (date('m', $birthday)) == date('m')){
        return 1;
    }else{
        return 0;
    }
}
add_shortcode( 'borntoday', 'born_today_func' );

So I switched back to what I put together before:

 function getbirthbymonth($month,$bdata) {if ($month == 0) {$now = time();$month = date('n',$now);}
if(strlen($bdata)==4){$bday = DateTime::createFromFormat('Y', $bdata);}else{$bday = date('n', strtotime($bdata));}
if ( $bday == $month) {return 1;}else{return 0;}}

This works faster than shortcode yet still too long to use.

I need to loop through everyone to display any member that have birthday.

Open to any solutions that displays birthdays of the day on home page properly.

#460109

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Ralph

There is no performant solution that doesn't involve cleaning up the source data to a standard format so that you can apply the test at the time of the query rather than looping across all actors and then performing a test against each and every one of them.

Tweaking the code for a test that shouldn't be performed isn't going to resolve the performance problem. There are no caching options for the view, if you wanted to go down that route you would need a bespoke solution, and you would be addressing the symptom and not the cause.

If your birth custom field can't be a date field then I suggest you add a date field and at the time the post is submitted or edited run a custom code snippet to try and parse the text that has been entered in your birth field and convert it into a date. You can then use that date field in your views as a query filter. Entries that fail to convert into a date (e.g. 1956) will be left blank and won't be output by your view. Add the custom field in Types and then use the slug with the wpcf- prefix when referring to the post meta key (e.g. 'wpcf-birthdate' for a Types field with a slug of 'birthdate').

You can use the save_post hook (https://codex.wordpress.org/Plugin_API/Action_Reference/save_post) to trigger your code so that whenever a post is created or updated (in the back end or on the front end via CRED) you can use the birth field and parse it into a date.

You can use strtotime() to try parsing the date (hidden link), but you may have to try enforcing some standards on your editors.

And you will need to write a script that will run once through all your existing actors etc. to create the date field entries.

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