Title: Managing Relationships Programmatically
Last modified: September 19, 2019

---

# Managing Relationships Programmatically

 *  Resolved [carlfost](https://wordpress.org/support/users/carlfost/)
 * (@carlfost)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/)
 * I am looking for a programmatic solution to managing relationships from php (
   $wpdb) or javascript (ajax), but I cannot find online any references or examples
   of how to do so.
    From the admin page, I was able to add a custom post_type called
   Registration and another one called Events. I set up a relationship field in 
   the Registration post_type called “registered_event”. From the admin screen I
   can drag and add or remove an event from the Registration post_type, but I would
   like to do the same programmatically. I cannot find a solution online how to 
   manage relationships programmatically. When I programmatically create a registration
   record in the wp_posts table and also create a record in the wp_postmeta table:
   metakey=”registered_event” and metavalue=”51” (which is the ID for the Event 
   record), the WordPress get_field(“registered_event”) command does not return 
   an array of the Event record. If I manually edit any field of the registration
   record from the admin screen and save it, then the get_field(“registered_event”)
   returns the correct data. How can I manage WordPress relationships programmatically
   in php ($wpdb) or javascripts (ajax)?
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fmanaging-relationships-programmatically%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/#post-11950499)
 * There is no built-in WordPress thing called relationship between two post types
   or two posts.
    If you are using a plugin to create the post types, you can ask
   your question at the plugin’s support forum. Otherwise, can you use a different
   term to describe what you want?
 *  Thread Starter [carlfost](https://wordpress.org/support/users/carlfost/)
 * (@carlfost)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/#post-11950595)
 * OK. It must be created by the Plugin Custom Design. I thought it was native to
   WordPress.
    This picture shows the difference between when I created the registration
   record from the admin screen, and when I programatically created it. [Relationship Field](http://www.outafire.com//wp-content/themes/outafire-theme/images/WordPressRelationship.png)
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/#post-11951405)
 * For the difference that you’re seeing in the database, you’ve stored an integer
   valur, while which ever theme/plugin is doing it for you it storing the value
   as a serialised array. All you have to do is store the value as a serialised 
   array too and you’ll see the same thing.
 *  Thread Starter [carlfost](https://wordpress.org/support/users/carlfost/)
 * (@carlfost)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/#post-11956308)
 * I found out that the Advance Custom Field (ACF) plugin setup the relationship
   field, and to update it, I use update_field(‘custom field name’,’related post
   ID, ‘wp_posts ID’);
    -  This reply was modified 6 years, 8 months ago by [carlfost](https://wordpress.org/support/users/carlfost/).

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

The topic ‘Managing Relationships Programmatically’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [carlfost](https://wordpress.org/support/users/carlfost/)
 * Last activity: [6 years, 8 months ago](https://wordpress.org/support/topic/managing-relationships-programmatically/#post-11956308)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
