Support

Account

Home Forums ACF PRO Auto populate new posts by submitting a different post type

Solved

Auto populate new posts by submitting a different post type

  • Hi All,

    I Have 2 custom post types “Timesheet” and “Payslip” both created with ACF Pro 5. I want to know if it is possible to create a new Payslip post every time a new Timesheet is created. I would like to get the field values from Timesheet and pass them to the new Payslip post as well.

    I appreciate your help in advance.
    Ed

  • Create an acf/save_post filter that runs after acf http://www.advancedcustomfields.com/resources/acfsave_post/

    Be sure to check the post type of the post just saved to prevent an infinite loop.

    
    add_action('acf/save_post', 'create_payslip');
    function create_payslip($post_id) {
      if (get_post_type($post_id) != 'timesheet') {
        return;
      }
      // get values from $post_id
      // use wp_insert_post() to create new post
      // use update_field() to update any acf fields
    } 
    
  • Hi John,

    Thanks. Much appreciated. It works perfectly.

    I just had one more question how do I prevent duplication of Payslips if the the same timesheet is updated.

    I want to make sure the data is updated. Currently every time I update the timesheet a new payslip with the same title is created.

    Cheers,
    Ed

  • sorry tried to mark your post as the solution 🙂

  • That would depend on how you’re matching them. You need to do a query or something to see if it already exists. How do you know which is which?

    Are you using a relationship field in the timesheet? Check to see if the relationship already exists. If yo give me more information about how you’re creating the new post I might be able to give you more information. But thinking about it now I’d say that adding a relationship field and updating it mighe be the easiest solution.

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

The topic ‘Auto populate new posts by submitting a different post type’ is closed to new replies.