CRED supports the parent-child relationship functionality that can be set up with the Types plugin. You can build forms that create child posts and set parents of these posts.
Let us assume that we have a website containing book reviews. We have created Book, Reader, and Review custom post types and set review as a child post of both Books and Readers. Therefore, Review is an intermediate object that connects Books and Reviewers and produces a many-to-many relationship.
Book Review Form
The CRED form for reviewing a book will input the review title and text, then select the reader that is reviewing and the book being reviewed. The form can be similar in appearance to the one in the following image.
Besides the form, a Review this book link inside each book’s page is also required. This link will take the user to a page containing this form. In this case, the parent book option will be pre-selected with the referred book.
Building the Form
We assume that you have already set up the parent-child relationship between the three custom post types in Types. We can now create a form that will operate on a Review post type.
Because CRED already knows that a Review belongs to a Book and Reader, the parents section will appear in CRED’s scaffold output. If you want to insert parent fields manually, use the Add Post Fields form button.
Displaying the Form
As with any CRED forms, the newly created form should appear with WordPress content, which is usually contained on a page. When you visit this page, you will notice that all parent selectors require input and are not pre-selected with any specific option.
An additional step involves displaying a review form, from which the book is preselected. Because we already have a page that includes the form, we want to link to it within the Books template and instruct CRED that the review will be applied to the referred book.
To achieve this, we must go to the Content template for the Books custom post type and insert a Create Child Post link using the CRED button.
If you visit a book’s page, you will now see the Review this book link. When you click on it, you will be redirected to the form for the book being referred.
Information about Parent Items
If you want to include information about the parent item inside the CRED form, you can use the following three shortcodes:
[cred_post_parent get='id'] [cred_post_parent get='title'] [cred_post_parent get='url']
These shortcodes also support a post_type attribute for selecting the parent manually.
If the post_type attribute is ommited, these shortcodes display information about the parent set by the referred page.
In that case, the above shortcodes need to be modified to the following:
[cred_post_parent get='id' post_type='slug_of_the_parent_you_want_to_target'] [cred_post_parent get='title' post_type='slug_of_the_parent_you_want_to_target'] [cred_post_parent get='url' post_type='slug_of_the_parent_you_want_to_target']
Finally, in our book reviews example, we can use the shortcodes to create a link back to the book:
Reviewing [cred_post_parent get='title'] <a href="[cred_post_parent get='url']">Back to [cred-post-parent get='title']</a>
The CRED form will now appear as follows: