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.
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?
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 intowp_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 awpdb->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 anupdate_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.