Skip Navigation

[Resolved] Prequel – sequel posts and multiple relationships in the same post sequencce

This support ticket is created 7 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#587636

Hey,
I'm trying to create and name a multitple 'one-to-one' or 'one-to-many' relationships for the same posts of the same type (or even different types).
It'll be easier to give the actual example:
In the database project that I'm working on, there are many relationships between many posts of many types.
The problem that I'm currently trying to tackle is "sequels/prequel" and similar relationships.
To be more concrete, I'd like to be able to set a post from type "Series" (let's call it "Series A") as a 'sequel' of another series (Series B). For that I can of course set the post type as a child of a linking post type, which is the parent of both Series A and Series B.
However, that wouldn't allow me to define that Series B is a prequel of Series A be it using a linking post or a custom taxonomy.

I followed the Types documentation (and several others) on relationships and it doesn't seem that such a problem has been tackled in the past.

An example (not on my site) - hidden link
The page above has quite a lot of series related to each other in different ways, and I'd like to do something similar.

Could you please advise a way to achieve what I'm trying to do?

Kind regards,
Amos

#587725

That's a pretty custom system of relationships. In Types, you could use repeating custom fields to define these relationships manually. In each post, you would place the ID of the related post in the custom field that describes that relationship. For example, in one post you could have the following custom fields and values:
Prequel: 12345
Prequel: 45678

Sequel: 23456

Same Setting: 0123
Same Setting: 9876

Alternate Setting: 3333
Alternate Setting: 222

... and so on...

Two things to note about this approach: these relationships are not reciprocal, and you have to know the post IDs. So adding a Prequel in one post does not automatically add Sequel to the reciprocal post. You have to maintain those relationships ad hoc in both locations. And there's not a "select post" custom field type, so there isn't a typeahead to search for posts. You must know the numeric IDs in order to make associations.

#587814

Thank you for the great advice!
Doing it by a field instead of through post relationships is something that I haven't thought of (for some reason, as you may recall helping me with setting the custom-IDs to be used for these exact situations).
I will describe what I did after that in case this helps anyone with a similar need in the future:
Building on that thought, I thought to myself - "That's good, but why use a field that cannot search through the post titles, when I could opt the title field obsolete by replacing it with a taxonomy?"
So instead, I created a custom taxonomy called "Series Titles" (sertl) (as opposed to Book Titles, Software Titles etc. which are also a part of the database) and removed the title field. This taxonomy has been assigned to the Series post-type itself, as well as a "Series relationship" (serrel) linking post-type, which has now been set as a child-post of the Series (ser) CPT (instead of parent as it was previously). Using this workaround, I can query the 'title' (taxonomy) of posts of the same type while setting the relation to them.
Extra note: this title taxonomy should be unique per post for the querying in views etc. to be effective.
In addition, I've added a select-type custom field of 'relationship type' (reltp) to the serrel CPT to avoid the repetitive need of naming those. This solves the note on the lack of 'select post' issue.
Extra tip: People using this workaround, do remember to enable 'show admin column' for the 'titles' taxonomy.

After that, several issues remained:

* Seeing that as per your fantastic idea on the previous topic, all posts will be created through CRED - I will be letting the post relations be done through CRED as well. I was thinking of maybe upon submission of a CRED relations form that creates a serrel with reltp: 3 (3 being 'sequel') for 'sertl: X' , I could use a hook that would find all ser CPT that belong to that sertl - and then create a serrel child-post with reltp: 2 (2 being 'prequel') and the sertl of the ser CPT, effectively making all those links reciprocal through hooks. Would that be possible? If so, could I ask for assistance in parsing it?

* Up until now the URL was based on the post title fields, however as mentioned I will now instead be using the taxonomy, so that titles will be left empy. Is it possible to use a taxonomy slug as a replacement for the title in the URL? (e.g. mysite.com/series/%titletaxonomyslug%) .

And on a somewhat related note - is it possible to enable filling custom term-fields through CRED? I have an 'alternative titles' text field on the titles taxonomy and can't quite figure out a way for users to contribute to it.

Sorry for the lengthy response and thank you so much for the assistance.

My best,
Amos

#587846

I was thinking of maybe upon submission of a CRED relations form that creates a serrel with reltp: 3 (3 being 'sequel') for 'sertl: X' , I could use a hook that would find all ser CPT that belong to that sertl - and then create a serrel child-post with reltp: 2 (2 being 'prequel') and the sertl of the ser CPT, effectively making all those links reciprocal through hooks.
Great idea. It seems like you'll need the following things:
- A CRED form that creates a new serrel, including the reltp input and the sertl term input.
This is pretty straightforward but I can help if you need assistance.

- A cred_save_data hook that listens for those submissions and finds all ser CPT with the same sertl term
You need to get_posts() using a tax query with the correct sertl term. Something like:

$args = array(
    'numberposts' => -1,
    'post_type' => 'cpt-slug',
    'tax_query' => array(
        array(
            'taxonomy' => 'tax-slug',
            'field' => 'slug',
            'terms' => $sometermvariable,
        )
    )
);
$sers = get_posts( $args );

- Code that loops over all those ser CPTs and creates a serrel child post for that ser post using the reciprocal reltp
So you need a foreach loop that loops over the posts from $sers and calls wp_insert_post to create the serrel post with the appropriate postmeta and tax information:

foreach( $sers as $ser ) {
  $newSerrel = array(
    // your post information here based on the docs for wp_insert_post();
  );
  wp_insert_post($newSerrel);
}

https://developer.wordpress.org/reference/functions/wp_insert_post/

Is it possible to use a taxonomy slug as a replacement for the title in the URL? (e.g. mysite.com/series/%titletaxonomyslug%) .
Permalink customization like this isn't something we support here in the forums, but I think it may be possible. Check out this article that discusses customization of permalinks using a custom taxonomy term:
hidden link
Otherwise, you could programmatically set the post slug using wp_update_post();
https://developer.wordpress.org/reference/functions/wp_update_post

is it possible to enable filling custom term-fields through CRED?
CRED is limited to Post and User management, unfortunately. But you could set up a CRED form that edits some unimportant post, captures input from your users, and uses cred_save_data to apply that input to any term fields as needed. That would require some custom code using the CRED API and WordPress update_term_meta: https://codex.wordpress.org/Function_Reference/update_term_meta