Currently using another plugin to access an API and it will obtain information needed for the child posts (Hazard Products).
The 3rd party plugin will add new posts and delete previously generated child posts if they are the same, disconnecting the relation between parent (Major Hazard) and child (Hazard Products) every time the plugin runs.
Expected results: I would like to avoid having to reconnect each child post to parent post individually. I want to have the child posts connect to the parent posts through the custom post field of hazard ID automatically when the 3rd party plugin generates new posts.
I want to have the child posts connect to the parent posts through the custom post field of hazard ID automatically when the 3rd party plugin generates new posts.
It sounds like you want to get the value of a custom field in the new child post, and use the value of that field to define the related Major Hazard post ID. Is this correct?
If so, you can access a Types custom field value in the database using get_post_meta and the Types field slug. If the Types field slug in wp-admin is hazard-id, then you can access the field value for the child post in the database using the postmeta key wpcf-hazard-id, as in this code:
$post_relationship_slug = 'major-hazard-hazard-product';
$new_hazard_product_id = 12345; // example child post ID variable
$hazard_field_value = get_post_meta( $new_hazard_product_id, 'wpcf-hazard-id', true);
You would replace hazard-id with the slug of the custom field in the child post, and replace $new_hazard_product_id with some variable representing the new Hazard Product (child post) ID.
Then you can use that field value to connect the child post and the parent post programmatically:
Just to clarify, the hazard_id field will be applied to both post types and will be already filled out and matching for the Major Hazard post and the Hazard Products posts. Would this still be the most direct way to make the connection?
If you want to use Toolset's Post Relationship feature, you must connect the posts using the toolset_connect_posts API. You can use custom fields to create your own relationships, but Views post relationship filters will not work and you will not be able to use Toolset's related posts features to easily access information in related posts. So it's up to you - you could use the custom field values as-is, or you could use the custom field values to automatically set the post relationship using Toolset's post relationships features with the toolset_connect_posts API. Yes, I think the examples I've shown are the most direct way of using custom fields to populate relationships using the post relationships APIs during programmatic post generation.
But this error is displayed:
<I>There was error when trying to re-run the snippet:E_NOTICE: Undefined variable: get_the_ID in---- on line 11 Function name must be a string A problem occurred when executing snippet "assign-hazard-products". The result of include_once is: ""</I>
What changes can I make to ensure that this will work?
I made that change but am now getting a different error:
There was error when trying to re-run the snippet:The role name to query by is not valid. Allowed values are: "parent", "child", "intermediary". A problem occurred when executing snippet "assign-hazard-products". The result of include_once is: ""
Hi Shane,
I wasn't getting anything back with the var_dump($children). The code will be under assign-hazard-products. The relationship is 'test-major-hazard-test-hazard-product'. I'm trying to get the custom fields of Hazard_ID in Test Major Hazard & Test Hazard Product to be automatically assigned.
No, I will need more help. The Parent Hazard ID & the Child post Hazard ID are populated through a 3rd party plugin. I would like to automatically connect the parent & child posts as long as these fields match.
We're using 3rd party plugin that will delete the old posts and generate new ones every hour based on the information in our API so I can't manually keep connecting these posts every hour. I would like for the previous code mentioned to find the values of the parent & child posts to automatically connect them.
So in summary all that is needed is to get the ID that is in the field and connect to the corresponding child post with the same value in the same field correct.
If so then we are going to need to use a little different query. We might even be able to use views to do the connection since views can return a list of posts based on the value of a field.