0

i create a new custom post type which i named "invoice" and then i create an new custom post type named "car" which contained a field "Km traveled" i want to update the field of "km traveled" of post "car" from the post type of "invoice". that's mean when i submit to insert/update the invoice the post of car must be updated. how can i make that? and if there is possible with example if exists

0

You need to use save_post action along with wp_insert_post() function for this. Something like following may work.

function tnc_update_car_details( $post_id, $post, $update ) {

    // If this is a revision, don't continue
    if ( wp_is_post_revision( $post_id ) )
        return;

    $post_type = get_post_type($post_id);

    // If this is not an invoice post type, don't continue
    if($post_type !== 'invoice')
        return;

    $car_id = '1';

    wp_insert_post(
        array(
            'ID' => $car_id,
            'meta_input' => array(
                'km_travelled' => '300',
            ),
        ),
    );
}
add_action( 'save_post', 'tnc_update_car_details', 10, 3 );

I've not tested though. Please let me know the result after trying.

  • is it possible to use that with this condition at that submit : if(isset($_POST['submit'])){ – Ammar.Dev Mar 19 '17 at 2:06
  • noticed that there was a mistake somehow. Just corrected that. with this code you don't need if(isset()). This will automatically work when you publish/update an invoice – Abdul Awal Mar 19 '17 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.