Skip Navigation

[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.)

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by SJ5362 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1151797

Tell us what you are trying to do?
Trying to create woo commerce products programmatically.

Is there any documentation that you are following?
No

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#1151862

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

You connect posts programmatically with the toolset_connect_posts API function (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts).

It takes 3 arguments, the slug of the relationship, plus the IDs of the two posts you are connecting.

#1151905
Capture.PNG

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.

#1151978

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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.

#1152008

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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.

#1152019

Hi,

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.

#1152023

My issue is resolved now. Thank you!