How are WordPress Post Relationships Created in the Database?

I’m using the advanced custom fields plugin to create relationships between custom post types. My custom post types are course and institution. So when I add a course post via the admin dashboard, I select an institution related to the course.

However now I am inserting posts programmatically via a wpdb object, and I want to create the same relationships. Therefore I need to know how the ACF plugin is creating relationships.

Read More

At the moment I think I should just insert extra data as post-meta for the course. But is this all that is needed? Do I not need to also update the institution to have the course?

Related posts

Leave a Reply

1 comment

  1. It’s better to use WP and the plugin’s API for that. Doing it with $wpdb is not a good idea, because other plugins may hook into wp_insert_post and if we use $wpdb it completely bypasses that. If we study the function, we can see that there’s a lot going on there, not only a wpdb->insert.

    ACF has its own logic to store meta data, simply using WP’s update_post_meta() is not enough.
    After doing a wp_insert_post(), use the returned ID to do an update_field().

    My point is: we don’t need to know “How are WordPress Post Relationships Created in the Database?”. We use the functions provided by the platform and we have a code that’s future proof.