Support

Account

Home Forums ACF PRO convert repeater field into custom post type

Solving

convert repeater field into custom post type

  • I’ve got a repeater field with too many rows (it slows down the admin interface).
    I’m thinking I should make a custom post type instead
    where each row in the repeater field turns into a blog post.

    What’s the easiest way to do that?

  • I cannot think of any easy way to do this.

    I would create the CPT and then I would create a function that did this that I would remove when the process is complete.

    I would also set the options page auto_load argument to true and save the options page, this alone might speed up the loading of the options page. But you need ti to auto load for the conversion.

    Next thing is that I would create a new text field in the repeater called “tranfered_to_post” or something like that, name does not really matter. Again, save the options page to and this will make sure the new sub field exists in all of the rows.

    
    add_action('init', 'move_repeater_to_cpt');
    function move_repeater_to_cpt() {
      // set some reasonable limit on the number of posts to create
      // to prevent timeout
      $limit = 20;
      // count of posts created
      $count = 0;
      if (have_rows('repeater', 'options')) {
        while(have_rows('repeater', 'options')) {
          the_row();
          if (!empty(get_sub_field('tranfered_to_post')) {
            // this row already done, move to the next one
            continue;
          }
          // *********************************************
          // create a new post base on row data
          // *********************************************
         
          // mark the row as done
          update_sub_field('tranfered_to_post, 'done');
        } // end while have_rows
      } // end if have_rows
    } // end function
    

    then you just need to keep reloading any page on the site until all of the rows are converted. Then delete the function.

  • Interesting. thanks for the idea and even code!

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

You must be logged in to reply to this topic.