• I’m learning WP using Local by Flywheel environment and using this plugin for the first time so enlightenment please before I tear my hair out.

    Note, I?have set the WP date format the same as the ACF plugin date format i.e. ‘d/m/y’.

    Code below sort of works. If I’m reading this right since the_field has an embedded echo it should print out the value of ‘event_date’ as a string argument

    $eventDate = new DateTime(the_field('event_date'));
    echo $eventDate->format('M');

    When I say sort of I meant it does not error out but produces this: the event date + publication month which is not what I want or expect at all. The only way to get the event date in the form of the month is to use this code….

    $eventDate = new DateTime(get_field('event_date', false, false));
    echo $eventDate->format('M');

    Code below does not. Again if I’m reading this right this returns a value which contains the date held in ‘event_date’. I really can’t see what the problem is here.

    $eventDate = new DateTime(get_field('event_date'));
    echo $eventDate->format('M');

    ERROR RETURNED IS:
    Upcoming Events
    FATAL ERROR: UNCAUGHT EXCEPTION: DATETIME::__CONSTRUCT(): FAILED TO PARSE TIME STRING (19/09/2019) AT POSITION 0 (1): UNEXPECTED CHARACTER IN /APP/PUBLIC/WP-CONTENT/THEMES/MYTHEME/FRONT-PAGE.PHP:34

    According to the documentation: the_field();
    Intuitive and powerful, this function can be used to output the value of any field from any location. Please note this function is the same as echo get_field();

    Additionally when I used this on another part of my page as a test it did not print anything out?
    <h1><?php the_field('event_date'); ?></h1>

    • This topic was modified 5 years, 9 months ago by GusGF.
    • This topic was modified 5 years, 9 months ago by GusGF.
    • This topic was modified 5 years, 9 months ago by GusGF.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Elliot Condon

    (@elliotcondon)

    Hi Gus.

    When loading a value to be used within another function, you will want to use the get_field() function. This returns the value instead of outputting it.

    I’m not sure exactly why your code is failing, but the clue will be in the PHP error logged. Be sure to investigate that error online.

    It will also be a good idea to check that the value returned is indeed a string, and not an empty result. Something like this would do:

    
    $value = get_field('event_date');
    if( $value ) {
        // Do something.
    }
    
    Thread Starter GusGF

    (@gusgf)

    Thank you for your help that explanation does indeed sort out my understanding ??

    userZ

    (@zakynthinos)

    For other users if you still have the same issue my solution was to change the default “Return Format” field at my Event Date (CF), on Edit Field Group section from (e.g). “18/07/2019” to “20190718”. Keep coding!! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘the_field vs get_field’ is closed to new replies.