Skip Navigation

[Résolu] populate a field with post_id

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

The main issue being described here is that the user wanted to update their custom field using php.

Solution:

To do this you will need to use the function in the link below.
https://codex.wordpress.org/Function_Reference/update_post_meta

A practical example of this can be seen below.


function save_post_func_2134( $post_id ) {     
    $post_type = 'doctor-order'; 
    if ( $post_type == get_post_type ( $post_id ) ) { 
        $my_post = array(
            'ID'           => $post_id,
            'order-id' => 'dts-' . $post_id,
            'post_title' => 'dts-' . $post_id
        );
        update_post_meta( $post_id, 'wpcf-order-id', 'dts-'.$post_id);
        remove_action('save_post', 'save_post_func_2134'); 
        wp_update_post( $my_post );     
    }
}
add_action( 'save_post', 'save_post_func_2134' );
This support ticket is created Il y a 4 années et 8 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : 

This topic contains 2 réponses, has 2 voix.

Last updated by Ido Angel Il y a 4 années et 8 mois.

Assisted by: Shane.

Auteur
Publications
#1292075

hey,

I already have added this code to create the post title by combining "dts-" + "post_id":

function save_post_func_2134( $post_id ) {     
    $post_type = 'doctor-order'; 
    if ( $post_type == get_post_type ( $post_id ) ) { 
        $my_post = array(
            'ID'           => $post_id,
            'post_title' => 'dts-' . $post_id
        );
        remove_action('save_post', 'save_post_func_2134'); 
        wp_update_post( $my_post );     
    }
}
add_action( 'save_post', 'save_post_func_2134' );

works great. but I also want to populate a custom field ("order-id") with the same combination.
I tried:

function save_post_func_2134( $post_id ) {     
    $post_type = 'doctor-order'; 
    if ( $post_type == get_post_type ( $post_id ) ) { 
        $my_post = array(
            'ID'           => $post_id,
            'order-id' => 'dts-' . $post_id,
            'post_title' => 'dts-' . $post_id
        );
        remove_action('save_post', 'save_post_func_2134'); 
        wp_update_post( $my_post );     
    }
}
add_action( 'save_post', 'save_post_func_2134' );

but didn't work.
any ideas?

thx!!

#1292137

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ido,

Thank you for contacting our support forum.

In order to do this you will need to to use the update_post_meta function.
https://codex.wordpress.org/Function_Reference/update_post_meta

So your code would look like

function save_post_func_2134( $post_id ) {     
    $post_type = 'doctor-order'; 
    if ( $post_type == get_post_type ( $post_id ) ) { 
        $my_post = array(
            'ID'           => $post_id,
            'order-id' => 'dts-' . $post_id,
            'post_title' => 'dts-' . $post_id
        );
        update_post_meta( $post_id, 'wpcf-order-id', 'dts-'.$post_id);
        remove_action('save_post', 'save_post_func_2134'); 
        wp_update_post( $my_post );     
    }
}
add_action( 'save_post', 'save_post_func_2134' );

Thanks,
Shane

#1292141

Perfect, Shane!
Thanks as always 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.