Forums Forums Search & Filter Pro Datefilter

Viewing 10 posts - 21 through 30 (of 32 total)
  • Trevor
    #238613

    An afterthought. The event you edit should be a past event.

    Anonymous
    #238615
    This reply has been marked as private.
    Anonymous
    #238617
    This reply has been marked as private.
    Trevor
    #238631
    This reply has been marked as private.
    Anonymous
    #238633
    This reply has been marked as private.
    Trevor
    #238865
    This reply has been marked as private.
    Anonymous
    #238867
    This reply has been marked as private.
    Trevor
    #238869

    So, in the child theme functions.php I have added this code at the end:

    function event_date_year_on_save( $post_id ) {
      $post_type = 'dig_veranstaltungen'; //custom post type for events
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the event date of the post. Note this is in YYYYMMDD format
      $event_date = get_post_field ('veranstaltungs_datum_beginn', $post_id);
    
      // update the event year value 
      update_field( 'jahr', substr( $event_date , 0, 4 ), $post_id );
    }
    add_action('save_post', 'event_date_year_on_save');

    It is really easy to get the year because it is the first four characters of the event date!

    Try it for yourself. Edit and event and change the event date (to a different year). No need to change the Year (jahr) you will see it changes automatically.

    I also added a little bit of CSS (in the theme customizer) to make the form nicer, and the year field also, plus German labels.

    Once you are happy that works, make these changes:

    https://www.screencast.com/t/yYKJTzoMp3

    Now the user will not see this field in the admin editor.

    All you have to do is edit the events and save. You do not actually need to make any changes, the save is to trigger the code to add the year.

    Anonymous
    #238894

    Yeah. This works. Perfect. Thank you.

    Will this work anyhow for
    2.) Events respectively in the current year (without mentioning the year. Just getting it from the current date)
    too?

    Trevor
    #238898

    Current year is harder, as it cannot be predefined. Is this on the same page of a different page, and by ‘current year’, do you mean only future events in the current year, OR past and future events?

    It would have a really odd effect as you approach the ‘next year’.

    Or, do you want the choices shown in the dropdown box to be:

    Current Year
    Next Year

Viewing 10 posts - 21 through 30 (of 32 total)