Problem: I am not able to find any fields from my Repeatable Field Group (RFG) when I try to select a dynamic source for a Block.
Solution: If you want to loop over RFGs, you'll need to use a View. Some blocks are designed to work with fields that allow multiple values, but not individual fields in an RFG. The type of block you are trying to insert will determine which type of "repeating" field is allowed for dynamic sources.
Problem: I would like to use a custom field from a related post to generate a dynamic link in the child post template. The Types field shortcode for this field is not returning any value.
Solution: You can use the Types field shortcode to output information from a related post using a special syntax in the "item" attribute, like this:
Problem: I have a one-to-many post relationship between two custom post types. In the template for the child post type, I would like to display "Next" and "Previous" links to paginate between the sibling posts.
Solution: Use a custom shortcode like this example that implements the post relationships API to create pagination links:
// https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/
// get a link to the next or previous sibling post in child post content template
// example: [tssupp-next-prev-child current="[wpv-post-id]" step="1" relslug="book-chapter"][/tssupp-next-prev-child]
// current: current post id, step: 1 for next or -1 for previous, relslug: slug of the post relationship
function tssupp_next_prev_child($atts) {
$a = shortcode_atts( array(
'current' => 0,
'step' => '1',
'relslug' => '',
), $atts );
$link = '';
// get all child posts
$relationship_slug = $a['relslug'];
$current_child_id = $a['current'];
$parent_id = toolset_get_related_post( $current_child_id, $relationship_slug );
$sibling_args = array(
'query_by_role'=>'parent',
'limit'=>1000,
'role_to_return'=>'child',
'orderby'=>'title'
);
$siblings = toolset_get_related_posts( $parent_id, $relationship_slug, $sibling_args );
// loop over child posts and get index of the current post
foreach($siblings as $i=>$sibling) {
if( $sibling == $current_child_id ) {
break;
}
}
// increment or decrement index for next or previous sibling
$i += $a['step'];
// create link to next/previous sibling
if(isset($siblings[$i])){
$perm = get_the_permalink( $siblings[$i] );
$title = get_the_title( $siblings[$i] );
$link .= $a['step']=='1' ? "Next: " : "Previous: ";
$link .= "<a href='" . $perm . "'>" . $title . "</a>";
}
// output the link, or empty string if not set
return $link;
}
add_shortcode( 'tssupp-next-prev-child', 'tssupp_next_prev_child' );
You'll use the shortcode like this to generate a "Next" link (if necessary):
Problem: I have a many-to-many (M2M) post relationship between two custom post types. I would like to be able to manage the custom fields applied to the relationship and also delete relationships from the front-end of the site using Forms.
Solution:
Create a Relationship Form to manage this M2M relationship.
Since there are some limitations with the Block Editor, the next step is to create an unassigned Content Template and choose to use the classic editor (link at the bottom of screenshot 1-classic-editor.png) to edit the template.
Then use the Forms button above the editor to insert the Relationship Form, and choose to create an "editing mode" template in the Form popup (2-editing-mode.png). Insert the shortcode and you'll see a shortcode appear in the template, like so:
Now you have an editing mode template for editing this relationship. In the loop of the View of related LCA Databases posts, you need to insert a Relationship Link to edit this relationship. The limitation here is it's not possible to generate this link from Blocks. Instead, you can use this shortcode as a template. Edit the code as described below and insert in a paragraph block:
[cred-relationship-form-link form='ranks-and-chapters' role_items='$fromViews' content_template_slug='ranks-chapters-editing-mode']Edit this CustInt[/cred-relationship-form-link]
Replace ranks-and-chapters with the slug of your Relationship Form. Replace ranks-chapters-editing-mode with the slug of the editing mode Content Template you just created. Replace Edit this CustInt with the text you want to display for the link.
Save the Tools Content Template and visit the Umberto Tool post on the front-end of the site. Now in the View of related LCA Databases you should see a new link to edit the relationship. Click the link to see the Edit Relationship Form, where you can manage the custom fields associated with this relationship.
To delete the relationship, use the Forms shortcode in the loop of the View of related posts:
Problem: I would like to know whether I should use a Post Reference field or a Post Relationship, and understand the differences between the two.
Solution: A post reference field is a very simple version of a one-to-many (O2M) relationship. In the database, they are handled identically. The main differences are how they are managed in wp-admin. For example, with a O2M relationship between Clients and Vehicles, you can edit the parent Client post and see all the child Vehicle posts easily in the post relationship editor panel. If you want to disconnect one of the Vehicle posts, you can do it from the Client post. However, with a post reference field, you cannot easily manage the related Vehicles from the Client post. You would have to edit each Vehicle post to change the post reference Client. So if you want more management capabilities in wp-admin, you should use a O2M relationship. If you do not need this type of management, you can use a Post Reference field instead.
If you need the ability to create multiple Client post references in the same Vehicle post, you should use a M2M relationship instead of a repeating post reference field.