Title: Programmatically adding a pod with a relationship field
Last modified: September 1, 2016

---

# Programmatically adding a pod with a relationship field

 *  Resolved [robbiejackson](https://wordpress.org/support/users/robbiejackson/)
 * (@robbiejackson)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/)
 * I’m developing a web site of walks, and have got a Walks custom post type pod
   to represent each walk. One of the fields is walk_grade which represents the 
   grade of difficulty, and I’ve used a Field Type of Relationship related to a 
   Grades custom taxonomy pod to represent that.
 * I need to add the walks pod records programmatically to implement the migration
   from a previous system, and that’s where I’ve been having difficulties with setting
   the walk_grade relationship field.
 * I’ve tried:
 *     ```
       $pod = pods('walk');
       $data = array(
           'post_title' => 'Test walk title',
           'post_content' => 'Short walk test description text',
           'town' => 'Townsville',
           'walk_grade' => 'Easy'    // tried it with and without this line
           );
       $id = $pod->add($data);     // this creates the pod with the title, content and town, but not grade
       $id = $pod->add_to('walk_grade', 'Easy');   // this doesn't work
       ```
   
 * Instead of the add_to call updating the walk with the grade of difficulty I find
   that it’s created a new walks pod which is basically empty (and I get back a 
   new $id).
 * Could someone please point me in the right direction?
 * Thanks,
    Robbie
 * [https://wordpress.org/plugins/pods/](https://wordpress.org/plugins/pods/)

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

 *  Plugin Contributor [Jim True](https://wordpress.org/support/users/jimtrue/)
 * (@jimtrue)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699490)
 * You sound like you’ve got flipped around. You shouldn’t use the walk_grade as
   a relationship field if it’s a taxonomy because that’s what it’s being treated
   as.
 * add_to takes an ID, not a ‘value’
 * [http://pods.io/docs/code/pods/add-to/](http://pods.io/docs/code/pods/add-to/)
 * So you’d need to get either the term_id of your taxonomy or the post id for the
   relationship.
 * if you associate walk_grade as your taxonomy with the walk post type, does the
   walk_grade => ‘Easy’ work?
 *  Thread Starter [robbiejackson](https://wordpress.org/support/users/robbiejackson/)
 * (@robbiejackson)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699498)
 * Many thanks for your help Jim, I think I’ve got it sorted now. I just read the
   add_to ($field, $value) signature and didn’t read the parameter definition closely
   enough!
 * In case it helps anyone else, I found the id which I needed using
 *     ```
       $grade = pods('grade');
       $params = array('where' => "name = 'Easy'");
       $grade->find($params);
       $result = $grade->fetch();
       $easy_id = $result['term_id'];
       ```
   
 * then
 *     ```
       $pod = pods('walk');
       $data = array(
           'post_title' => 'Test walk title',
           'post_content' => 'Short walk test description text',
           'town' => 'Townsville',
           'walk_grade' => $easy_id
           );
       $id = $pod->add($data);     // this creates the walks pod, including the grade ok
       $id = $pod->add_to('walk_grade', $easy_id);   // inserts the appropriate wp_pods_rel relationship record
       ```
   
 * I still get the blank Walks pod record created as a result of the add_to() call,
   but I can easily remove that with a subsequent delete() call.
 * Robbie
 *  Plugin Contributor [Jim True](https://wordpress.org/support/users/jimtrue/)
 * (@jimtrue)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699503)
 * I think add_to is intended to be used only when you’re adding a new relationship
   to an existing relationship field (ie like push’ing a value onto an array).
 * If this is the only possible ‘grade’ for this walk, just use $pod->add() to save
   your record to ‘walk’ and the relationship field will be set automatically as
   part of adding that record. I believe you’re actually saving the relationship
   by setting it in your ‘walk_grade’ => $easy_id.
 * You don’t have to then turn around and add it again.
 *  Thread Starter [robbiejackson](https://wordpress.org/support/users/robbiejackson/)
 * (@robbiejackson)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699508)
 * Jim, I’ve checked the wp_podsrel records and you’re quite correct, the call to
   add() sets the appropriate record there as well.
 * So the call to add_to() is superfluous. And taking this out would remove that
   blank Walks record that appears when it’s called.
 * Many thanks again,
 * Robbie
 *  Plugin Contributor [Jim True](https://wordpress.org/support/users/jimtrue/)
 * (@jimtrue)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699564)
 * You’re quite welcome. I had a feeling it looked like you were doing a double-
   call.

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

The topic ‘Programmatically adding a pod with a relationship field’ is closed to
new replies.

 * ![](https://ps.w.org/pods/assets/icon.svg?rev=3286397)
 * [Pods - Custom Content Types and Fields](https://wordpress.org/plugins/pods/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pods/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pods/)
 * [Active Topics](https://wordpress.org/support/plugin/pods/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pods/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pods/reviews/)

## Tags

 * [custom taxonomy](https://wordpress.org/support/topic-tag/custom-taxonomy/)

 * 5 replies
 * 2 participants
 * Last reply from: [Jim True](https://wordpress.org/support/users/jimtrue/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-pod-with-a-relationship-field/#post-7699564)
 * Status: resolved