Tell us what you are trying to do?
Calculate the number of days to harvest (PARENT slug: plante-recolte) in the hydroponics system based on transplant date (slug: semis-date-transplantation).
Under Toolet > Custom code I added this snippet
/**
* Calculate the number of days to harvest (PARENT slug: plante-recolte) in the DFT system based on transplant date (slug: semis-date-transplantation)
The shortcode will look like this
[dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
In a page , I inserted a view using the shortcode [dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]
Result: The "not empty" condition does not work. The view displays the current date when the date_transplant or the days_harvest is empty (As shown next to Récolte prévue: ). I tried adding a conditional block to the view with the "not empty" condition. Result is the same.
Is there any documentation that you are following?
In the screen shot I provided, I inserted the following fields :
Transplanté: (date) (this a single field representing the transplant date)
Jours pour récolte : (number) (this is the number of days to harvest - pulled from the plant relationship)
Récolte prévue: (shortcode) This is where I inserted the shortcode
If either the transplant date or days to harvest are empty, Récolte prévue should be empty. In the screenshot, you can see that it's returning today's date when a field is empty. Meaning it's not correctly assessing the Not IsEmpty condition.
I'm struggling to find where the custom field below is located.
[types field='plante-recolte' item='@plante-semences.parent'][/types]
I'm not seeing it on the parent post type (Semis) . Can you point me to exactly where this field is. I'm doing a trace of the field to find out what value is being stored in the field.
That custom field is in custom post Plante (slug: plante) - under field group Guide de culture (culvitation guide).
In Summary: I created a custom post for plants (slug:plante) and another custom post for seedlings (slug: semis). Then, I created a one-to-many relationship (slug: plante-semences)
The plant contains the information about the number of days to harvest. The seedling contains the date of transplant of the seedling.
After further checks the issue is not that the dates were empty. It's that you were calling the date_i18n function outside the if statement and in this case it will default to Today's date when no timestamp is provided.
The correct code is below.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_shortcode('dft-harvest-date', function($atts, $content){
$atts = shortcode_atts( array(
'date_transplant' => '',
'days_harvest' => ''
), $atts );
$res = '';
if( !empty($atts['date_transplant']) && !empty($atts['days_harvest']) ){
$ts = strtotime( '+' . $atts['days_harvest'] . ' days', $atts['date_transplant']);
$res = date_i18n( 'j-M-Y', $ts );
}
return $res;
});
In a page , I inserted a view using the shortcode [dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]
Given that the date should only be there if both fields are not empty then placing the date function inside the if condition as well will ensure this.