Skip Navigation

[Resolved] Create relation between custom posts based on slug beginning

This support ticket is created 4 years, 6 months 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.

Our next available supporter will start replying to tickets in about 0.09 hours from now. Thank you for your understanding.

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+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 4 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1354523

I have this huge film database and there are lots of other custom types also and it would be great to link these together. But building a relation and manually creating the relation is out of the question. So I'm thinking of 2 possibilities here:
1. Somehow program the relation creation
2. "Fake relation", that is explained below.

Example case: connect the films and festivals.
Film CPT: film name (cpt title) + other fields
Festival performance CPT: film name (cpt title) + other fields

Now we have a film called "Promise" and it has 10 different festival performances. These are all called "Promise" and the slugs are promise-1, promise-2, and so on. Could this be somehow used here, like create a shortcode that strips the ending -xx from the slug and with this I could somehow create the link between the film and festival.

#1354707

Nigel
Supporter

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

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

I'm guessing that the objective here is to do this once, for the existing content, to automatically create such connections, and that you would be happy to manually connect posts when new content is added?

In any case, the solution is largely the same, but it requires writing a custom PHP script that utilises the Relationships API to connect posts.

I can give you an outline of what is involved, but I can't write the code for you, so if you are not comfortable with PHP you will need to hire a developer (see https://toolset.com/contractors).

Your script would need to get all of the film posts and then iterate over them. For each film post you would want to search for festival performance posts with a matching slug. Because of limitations with WP_Query I think you would need to construct your own SQL query using the wpdb class for such a search (unless there are custom field values that would connect the film and festival performance posts rather than the slug).

https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/classes/wpdb/prepare/

This, so far, is generic WordPress programming that any developer should be able to do.

The next part is to connect the matching film and festival performance posts, for which you would use the toolset_connect_posts API function (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts).

Depending on how content is being added to your site, you could automate the process of creating connections using a similar technique, possibly triggered by the cred_save_data API hook if you are submitting content through front-end forms, or the save_post WordPress hook (https://developer.wordpress.org/reference/hooks/save_post/).

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.