Loop through several custom post types to update post_title using a custom field

Support MB Custom Post Type Loop through several custom post types to update post_title using a custom field

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #42187
    HelmsHelms
    Participant

    Hi all. Newbie here. I searched the forum and used this code successfully to update the service post type's post_title using a custom field. How would I rewrite this to support looping through multiple custom post types (service, reviews, portfolio, etc.) and have their custom fields update that specific post type's post_title when a new post is created?

    
    <?php
    
    add_action("rwmb_service_after_save_post", "update_post_title");
    function update_post_title($post_id)
    {
        // Get the field value
        $my_meta = rwmb_meta("service_title", "", $post_id);
    
        // Preprare update post
        $my_post = [
            "ID" => $post_id,
            "post_title" => $my_meta,
        ];
    
        wp_update_post($my_post);
    }
    

    Thanks in advance.

    #42197
    PeterPeter
    Moderator

    Hello,

    The service in the action hook should be the field group ID, not the post type slug. Please read more in the documentation rwmb_{$field_group_id}_after_save_post.

    If you assign one field group for multiple post types, you can use one block of code to modify the post title of other post types.

    #42210
    HelmsHelms
    Participant

    Hi Peter,

    The service in the action hook is my field group ID, not the slug. I will try what you suggested and reply back. Thanks.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.