Support

Account

Home Forums Add-ons Repeater Field Using the repeater field with non WordPress DB table

Helping

Using the repeater field with non WordPress DB table

  • I’ve created a custom table in the WordPress DB with all the needed functions for working with the table (insert,delete,update etc…)

    I also created a custom post type, on which I wish to add a GUI and functionality for working with the data of this table (in the back-end).

    What I want to do is to add a repeater field containing the rows from the DB – basically a “regular” repeater, just with field from my own DB table.

    How can one do such thing?

  • Dynamically populating a repeater field is a somewhat complicated process. I used to have some code laying about but I can’t located it now. I can give you a general outline. If you can create a custom DB in WP the you should be able to work it out the details.

    You need to use the acf/load_value/key={$field_key} option of the load_value hook, that is, using field keys rather than field names. http://www.advancedcustomfields.com/resources/acfload_value/

    Step one: create a PHP class. You’re going to need a class (or at least I did) because you will need to keep track of how many times each of your functions has been called. So you need to be able to set some class values to keep a count for each function.

    Step 2: Create a filter function that is called when the repeater field is loaded. You will need to detect if this is a new post or an existing post. You only want to dynamically generate values when someone is creating a new post and not when an existing post is being loaded or you will overwrite whatever was previously saved.

    If a new post being created then return the number of rows you want to create, otherwise return the value without altering it.

    Step 3: Create a filter function for each sub field in the repeater. This function must keep track of how many times it was called and each time it is called it should return then next value from your list of values that you get from your custom table.

    This function, like the one for the repeater itself, should only alter the field value if a new post is being created and not when an existing post is being loaded. Again, if it is an existing post then return the field value without altering it.

    Hope this helps.

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

The topic ‘Using the repeater field with non WordPress DB table’ is closed to new replies.