Support

Account

Home Forums Backend Issues (wp-admin) Need help with date function

Unread

Need help with date function

  • Hi there,

    I want to autogenerate title and slug from ACF fields and terms and I’ve already created a function which works nearly perfect. My only issue is, the date output is not working correctly. I don’t want the present date but the correct date from the two custom_fields from date picker. Here is the custom code:

    
    // Create Auto Title for 'Veranstaltungen'
    add_action('save_post', 'wpb_autogenerate_events_title', 10, 3);
    
    function wpb_autogenerate_events_title($post_id, $post, $update) {
        // Check if it's the 'events' CPT and if the title is empty.
        if ('veranstaltungen' !== $post->post_type || !empty($post->post_title)) {
            return;
        }
    
        // Fetch terms from 'category' and 'nameselection' taxonomies.
        $category_term = wp_get_post_terms($post_id, 'veranstaltungskategorie', ['fields' => 'names']);
        $nameselection_term = wp_get_post_terms($post_id, 'winzertax', ['fields' => 'names']);
    
        // Fetch the 'date' custom field created with ACF Pro.
        $date_start = get_field('datum_beginn', $post_id);
    	$date_end = get_field('datum_ende', $post_id);
    	
    	$date_start_form = date( "dmY", strtotime($date_start));
    	$date_end_form = date( "dmY", strtotime($date_end));
    
        // Combine the terms and the date to form the title.
        $new_title = join(' <br>bei ', array_merge((array) $category_term, (array) $nameselection_term));
    	$new_titleslug = join('-', array_merge((array) $category_term, (array) $nameselection_term, (array) $date_start_form, (array) $date_end_form));
    	
        // Prepare the slug from the title.
        $new_slug = sanitize_title($new_titleslug);
    
        // Update the post.
        wp_update_post([
            'ID' => $post_id,
            'post_title' => $new_title,
            'post_name' => $new_slug, // This is the slug.
        ]);
    }
    

    Could you please help me why the output isn’t correct?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.