Skip Navigation

[Gelöst] Using PHP to attach Hazard Product(CPT) posts to the Major Hazard(CPT) parent.

This support ticket is created vor 3 Jahre, 8 Monate. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 35 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Luigi Bella vor 3 Jahre, 6 Monate.

Assistiert von: Shane.

Author
Artikel
#1788199

CPF Slugs: hazard_id
Relationship Slug: major-hazard-hazard-product

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.

#1789411

Hello, it sounds like you need assistance connecting two posts using the post relationships API. We have documentation for this API available here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_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:

toolset_connect_posts( $post_relationship_slug, $hazard_field_value, $new_hazard_product_id );

Please let me know if I have misunderstood your request, and I will give you more feedback.

#1789591

Thank you for your help, Christian!

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?

#1789625

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.

#1790915

Hi Christian,

I would like to avoid manually inputting a child post ID variable. I tried the following code but not sure if it's correct.

$post_relationship_slug ='major-hazard-hazard-product';
$child = get_post_meta($get_the_ID(), 'wpcf-hazard-id', true); 
$parent = get_post_meta($child, 'wpcf-hazard-id', true);
$children = toolset_get_related_posts($child,'major-hazard-hazard-product');

foreach ($children as $child) { 
toolset_connect_posts($post_relationship_slug, $parent, $child);
}

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?

#1794649

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

From what I see you're calling the get_the_ID() function like a variable, so you should change it from.

$get_the_ID()

To

get_the_ID()

Please let me know if this helps.
Thanks,
Shane

#1794763

Hi Shane,

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: ""

This is my code currently:

$post_relationship_slug ='test-major-hazard-test-hazard-product';

$child = get_post_meta(get_the_ID(), 'wpcf-hazard-id', true); 
$parent = get_post_meta($child, 'wpcf-hazard-id', true);

$children = toolset_get_related_posts($child,'major-hazard-hazard-product');

foreach ($children as $child) { 
toolset_connect_posts($post_relationship_slug, $parent, $child);
}

Thank you.

#1797063

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

From what I can see we need to dump out what is in the $children variable.

Can you do a var_dump($children); then let me know what gets dumped onto the page when you are viewing it ?

I'm not sure where exactly this code is failing.

If you would like me to log in and test it out you can let me know and I'll enable the private fields for you.

Thanks,
Shane

#1797119

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.

Thanks for your help

#1797131

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

I've enabled the private fields for your next response so that you can securely provide me with the login credentials.

Also please let me know the URL of a post where I can clearly test this code out. Finally let me know where the code is located.

Thanks,
Shane

#1801605

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

I tried to have a look at this for you but it seems the test Hazard product was deleted.

I also tried to find the product based on the Hazard ID that was entered in the post here versteckter Link

However i'm not able to find the post to do my further checks.

Thanks,
Shane

#1802123
Screen Shot 2020-10-06 at 8.07.01 AM.png

Hi Shane,

Sorry for the confusion. The test hazard products are any of the posts that display as "TC Marie." Are you able to check again?

Thank you

#1806441

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

Thank you I believe I understand this correctly now, you actually want to add the Parent Hazard ID value into each of the Child post Hazard ID field.

Checking on your test post here I see that the fields on the child posts now have the correct Hazard ID.
versteckter Link

Can you let me know if my assistance is still needed.

Thanks,
Shane

#1806447

Hi Shane,

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.

Thank you

#1809163

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Luigi,

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.

Please let me know and we can take it from there.

Thanks,
Shane

Dieses Ticket ist jetzt geschlossen. Wenn Sie ein Toolset Kunde sind und Hilfe benötigen, eröffnen Sie bitte ein neues Support-Ticket.