Support

Account

Home Forums Front-end Issues Update post without changing its title

Solving

Update post without changing its title

  • Hi,

    I am trying to implement form to post, which will update this post with shop details.
    This form is of relation type.

    So I pasted this:

    <?php
    	/*
    	acf_enqueue_uploader();
    	$options = array(
    		'post_id' => get_the_ID(),
    		'field_groups' => array( 37 ), // Grupy z ACFa
    		'form' => true, // set this to false to prevent the <form> tag from being created
    		'form_attributes' => array( // attributes will be added to the form element
    			'id' => 'post',
    			'class' => '',
    			'action' => '',
    			'method' => 'post',
    		),
    		'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
    		'html_before_fields' => '', // html inside form before fields
    		'html_after_fields' => '', // html inside form after fields
    		'submit_value' => 'Zapisz', // value for submit field
    		//'updated_message' => '<h4 class="alert alert-success">Zapisano</h4>', // Potwierdzenie.
    		'field_el' => 'div',
    	);
    	
    	 acf_form( $options ); */?>

    And it almost works, almost because it also deletes my posts title.
    So shop is added to the post, but title is being deleted.

    What am I doing wrong here ?

  • I have notices that I have this function in my single.php

    add_action( 'acf/save_post', 'my_update_existing_post_data', 10 ); 
    	function my_update_existing_post_data( $post_id ) {
    
    		// Update existing post
    		$post = array(
    			'ID'           => $post_id,
    			'post_status'  => 'publish',
    			'post_title'   => wp_strip_all_tags($_POST['acf']['field_5464a2a2d9a50']), // Post Title ACF field key
    		);
    
    		// Update the post
    		$post_id = wp_update_post( $post );
    	}

    This is used to update posts title, but this also is used when editing with the form responsible for updating the shop details. And because of that I get my title empty.

    Any idea how to make those 2 work together ?

  • Hi @mastafu

    I think you could just do a check for the title value in your save_post hook and ignore the changes if it’s empty.

    
    add_action( 'acf/save_post', 'my_update_existing_post_data', 10 ); 
    function my_update_existing_post_data( $post_id ) {
    	
    	if(!isset($_POST['acf']['field_5464a2a2d9a50']) || $_POST['acf']['field_5464a2a2d9a50'] == '')
    		return;
    		
    	
    	// Update existing post
    	$post = array(
    		'ID'           => $post_id,
    		'post_status'  => 'publish',
    		'post_title'   => wp_strip_all_tags($_POST['acf']['field_5464a2a2d9a50']), // Post Title ACF field key
    	);
    
    	// Update the post
    	$post_id = wp_update_post( $post );
    }
    
    

    Let me know how that works!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Update post without changing its title’ is closed to new replies.