Support

Account

Home Forums General Issues How to change 'post_status' and 'meta_input' of already created post?

Solved

How to change 'post_status' and 'meta_input' of already created post?

  • Hi all. Please tell me, is it possible to change ‘post_status’, ‘data_input’ and other parameters of wp_insert_post?

    Now I’m using this code to create a new record and it works:

        <?php
        global $current_user;
        wp_get_current_user();
        acf_form(array(
            'post_id'       => 'new_post',
            'post_title' => true,
            'fields' => array('task_url','task_where','task_region','task_tariff','task_count','task_otherinfo'),
            'new_post'      => array(
                'post_type'     => 'task',
                'post_status'   => 'waitpay',
                'meta_input' => array(
                    'task_customer' => $current_user->ID,
                    ),
            ),
            'return' => 'profile',
            'submit_value'  => 'Create'
        )); ?>

    Next, you need to display one button, by pressing which this created post’ post_status ‘ is changed to ‘inwork’, and in the field ‘task_worker’ is recorded the id of the current user. I’m using the following code and it doesn’t work (The field ‘none’ in ‘fields’ does not exist, I just specified it so that no fields are displayed and only the button is displayed.):

                <?php global $current_user;
                wp_get_current_user();
                $userid = $current_user->ID;
                $idtask = get_the_ID();
                acf_form(array(
                    'post_id' => false,
                    'new_post' => array(
                        'post_status' => 'inwork',
                        'meta_input' => array('task_worker' => $userid, 'task_result' => '')
                        ),
                    'fields'	=> array('none'),
    			    'submit_value'	=> 'Take'
    			    )); ?>

    How to force ACF to change post_status and other fields? In the creation form, everything works if you specify in ‘new_post’, but you can not edit it. Tried instead of ‘new_post’ to specify id of the current record, but so too does not work.

  • for post ID you need to supply the id of the post you are editing, ACF may not be able to figure that out unless the form is located inside “The Loop” in “single-task.php”.

    Then you need to create an acf/save_post filter to update the post status and other information. ACF will not do this for you. https://www.advancedcustomfields.com/resources/acf-save_post/

  • Thanks for the answer. Everything worked, only with acf/pre_save_post, not acf/save_post.
    The code from the functions.php

    function my_pre_save_post($post_id) {
      
        if (!is_numeric($post_id) || get_post_type($post_id) != 'task') {
        return $post_id;
        }
    	global $current_user;
        wp_get_current_user();
    	$userid = $current_user->ID;
        $post = array(
        	'ID' => $post_id,
        	'post_status'  => 'inwork' ,
        	'meta_input' => array('task_worker' => $userid, 'task_result' => ''),
        );  
    
        $post_id = wp_insert_post($post); 
        wp_update_post($post);
        
        return $post_id;
      }
      
      add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    Prompt, and how to make if buttons two? The button whose code is given above changes ‘post_status ‘to’ inwork ‘and’ meta_input ‘ to the current user id. The other button should change ‘post_status’ to ‘pending’.

  • In your filter you would need to test to see what values in $_POST are set and set the status based on that. Each button would need to product input that could be tested for.

  • Everything worked, did like this. Thanks for the help.

    function my_pre_save_post($post_id) {
      
        if (!is_numeric($post_id) || get_post_type($post_id) != 'task') {
        return $post_id;
        }
    	global $current_user;
        wp_get_current_user();
    	$userid = $current_user->ID;
    	if( is_page( 21 ) ){
    		$post = array(
    			'ID' => $post_id,
        		'post_type' => 'task',
        		'post_status'  => 'inwork' ,
        		'meta_input' => array('task_worker' => $userid, 'task_result' => ''),
        		);
    	$post_id = wp_insert_post($post); 
        wp_update_post($post);
    	}
        if( is_page( array(14, 82) ) ){
    		$post = array(
    			'ID' => $post_id,
        		'post_type' => 'task',
        		'post_status'  => 'pending' ,
        		);
    	$post_id = wp_insert_post($post); 
        wp_update_post($post);
        }
        
        return $post_id;
      }
      
      add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How to change 'post_status' and 'meta_input' of already created post?’ is closed to new replies.