Support

Account

Home Forums General Issues Prepopulate Fields from Other CPT

Solving

Prepopulate Fields from Other CPT

  • I have a few different custom post types. Can I get data which is saved in cpt A to prepopulate fields in cpt B? Or have the two cpts share those data?

    The issue is that I want every cpt B to be searchable by things like contact info, rather than have to enter the same data every time I figured it should be saved as its own post type (cpt A) which then just shares its fields, or data.

  • Hi @yosef

    You can use update_field() in the acf/save_post to update the fields in CPT B based on the CPT A.

    You can also query CPT A first on search, get the returned IDs, then query the CPT B based on the returned IDs. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/.

    I hope this makes sense. Thanks!

  • I decided to pull the data from wp_option table instead so that the client will have an easy to find place to update contact information.

    Now, I have tried many variations on this. The variable is getting the data from option but the field is not updating. As you can see I wan this to work with several fields but I’m starting with just the name because I cannot even get that to work. Any idea what I’m doing wrong here?

    
    function add_contact_info_to_post( $post_id ) {
      $mm_restaurant_name = the_field('mm_restaurant_name', 'option');
      $mm_phone = the_field('mm_phone', 'option');
      $mm_email = the_field('mm_email', 'option');
      $mm_location = the_field('mm_location', 'option');
    
      $field_key = 'field_56e5f9f891134';
      update_field($field_key, $mm_restaurant_name);
    }
    add_filter( 'acf/save_post', 'add_contact_info_to_post' );
  • I figured it out. I just switched the_field('mm_restaurant_name', 'option'); to get_field('mm_restaurant_name', 'option'); and now it works just fine. Thank you for your help!

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

The topic ‘Prepopulate Fields from Other CPT’ is closed to new replies.