Skip Navigation

[Resuelto] Conditional statement when ACF date field is greater than today

This support ticket is created hace 4 años, 8 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Etiquetado: ,

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por ellenB hace 4 años, 8 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1513507

I need some assistance on creating a conditional statement in my Product Archive.

I would like to display an image if an ACF date field is greater than today.

I have done the following:

-- Confirmed that the ACF date field as stored in the database (hardback_pub_date) is numeric (e.g. 20200315 for March 15, 2020)
-- Created a shortcode to produce a numeric value for today (e.g. 20200217)
-- Registered this shortcode with Toolset and confirmed that produces the numeric field
-- Created the conditional statement in my Product Archive

The relevant code I added is below.

"Now" Shortcode:

function now_func( $atts ){
      return date("Ymd");
}
 add_shortcode( 'now', 'now_func' );

Conditional statement:

[wpv-conditional if="( $(hardback_pub_date) gte '[now]' ) )"]
       <p>test text</p>
    	<img src='/PATH.png'/>
[/wpv-conditional]

Unfortunately, this is not currently working. Here is what I have done for troubleshooting:

-- Confirmed that the image source is working by adding it to another conditional statement. It displayed without any issue in the working conditional.
-- Changed the conditional from gte to lt (as that would then encompass nearly every product in the shop) and the image and test text did not display.
-- Temporarily removed the other conditionals on the page (which display the "Out of Stock" and "On Sale" images) in case they were conflicting with the new conditional but the image and test text still do not display.

My site is enlace oculto. The password to view the site is "WeLoveBooks".

My shop page URL is enlace oculto. The first product on that page, "Dance, Nana, Dance / Baila, Nana, Baila", has a hardback_pub_date value of 20200324 in the database.

#1514087

Hello,

I assume the ACF plugin stores custom date field value in string format, for example: 2020-2-19.

You can try to turn the string value into timestamp format, like this:
1) Add below PHP codes in your theme file "functions.php":

add_shortcode( 'str-to-time', function($atts, $content){
	$atts = shortcode_atts( array(
		'field' => 'hardback_pub_date',
	), $atts );	
	$str = get_post_meta(get_the_ID(), $atts['field'], true);
	return strtotime ( $str );
});

2) Dashboard-> Toolset-> Setting-> Front-end Content
in section "Third-party shortcode arguments", add above shortcode name: str-to-time

3) Setup the shortcode like this:

[wpv-conditional if="( '[str-to-time]' >= now() )" debug="true"]
       <p>test text</p>
        <img src="/PATH.png">
[/wpv-conditional]

More help:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/

#1518837

The value that I see in the database for the ACF custom date fields is in the Ymd format, for example: 20200219 but apparently it doesn't output that way when I call the hardback_pub_date in Toolset.

I went ahead and created another shortcode similar to the one you proposed that returns a Ymd format and then compare that to the [now] shortcode.

My shortcodes (located in functions.php and registered with Toolset at Settings > Front-end Content > Third-party shortcode arguments):

// Hardback Publication Date in Ymd format
add_shortcode( 'hardback-pub-date', 'hardback_pub_date_func' );
function hardback_pub_date_func($atts, $content) {
  $atts = shortcode_atts( array(
    'field' => 'hardback_pub_date',
  ), $atts ); 
  $str = get_post_meta(get_the_ID(), $atts['field'], true);
  return $str;
}
// Now in Ymd format
add_shortcode( 'now', 'now_func' );
function now_func( $atts ){
  return date("Ymd");
}

My conditional statement in the Toolset archive:

[wpv-conditional if="(  [hardback-pub-date] gte [now]  )" ]
    <img src='/PATH.png'/>
[/wpv-conditional]  

This is working now. I appreciate your help!

#1518839

My issue is resolved now. Thank you!