[Resolved] Programmatically add post relationship field value when doing wp_insert_post
This thread is resolved. Here is a description of the problem and solution.
Problem:
How to programmatically connect posts using a post reference field.
Solution:
There is some duplication with the post reference field, it is stored both as post meta (using wpcf-field-slug as key) but also in the associations table Toolset manages post relationships with, and so as well as setting the post meta value it is also necessary to use toolset_connect_posts to join the posts. (The first argument is the slug of the post relationship field.)
Post Reference and Post relation are they same?
I have custom fields of type post reference, and I am trying to import products from csv and for that I have created custom plugin and when I check the wp_termmeta table in database meta value is being updated, but the wp_toolset_associations table does not have any row for the relationship.
When I looked at this before I recall that adding a post reference (when saving a post in the backend) added both an entry in wp_postmeta and an entry in wp_toolset_associations (and, according to the devs, the latter was key). The relationship slug would be the slugs of the two post types (e.g. 'owner-dog').
I'll need to do some more testing to confirm that is the case, but you can check in the meantime what happens when you save such a post from the post edit screen, that is what you would need to reproduce programmatically.
Quick update to say that I tested this and my recollection was nearly correct.
The post reference is stored in wp_postmeta and is also duplicated in the associations table, but the slug of the relationship you would use when setting up the connection with toolset_connect_posts would be the same as the slug of the post reference field.
I tried the below code which returned "this relationship doesn't exits"
toolset_connect_posts('product-book-author',$author,$post_id)
where
product is post_type for woocommerce product
book-author is post type for custom post type
$author is post id of author post
$post_id is post id of woocommerce product.
According to you the slug I used is wrong. Can you please confirm?
The post reference field slugs in my site is "author"
Let me know what would be the correct slug to use.