Hi,
Thank you for contacting us and I'd be happy to assist.
From your message, I understand that you're using the "add new biography" form to also connect the related person/company posts.
To make sure that only non-connected person/company posts can be connected, there are two options:
1. A simpler workaround is to not connect the related person/company posts through the form that creates a new biography. Instead, you can use a separate relationship form at the next step, which will not allow connecting post that already has a connection.
OR
2. If you'd prefer to use the same form that creates a new biography for this, you'll need to add some customizations:
a). You'll remove the relationship field to select the related person/company and instead add a select type generic field. The options in this field can be programmatically created from the available person/company posts, excluding the ones, which are already connected.
Here is an example of a custom shortcode to generate this field's options:
add_shortcode('add_people_field', 'add_people_field_func');
function add_people_field_func() {
// get all the people posts
$args = array(
'post_type' => 'people_post_type_slug',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
// check if a related post already exists
$related_id = toolset_get_related_post( $post->ID, 'people-biography_relationship_slug' );
// if it doesn't include in the options
if(!$related_id) {
$data[] = array('value' => $post->ID, 'label' => $post->post_title );
}
}
// return the them in json format for the generic field
return(json_encode($data));
}
Note: You'll replace "people_post_type_slug" with the actual slug of your person/company post type and "people-biography_relationship_slug" with the slug of the relationship between them.
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
b). In the form to add a new biography the generic field code will look like this:
[cred_generic_field type='select' field='people-field']
{
"options":[add_people_field]
}
[/cred_generic_field]
As a result, you'll have a select field in the form that will only show the person/company posts which are not already connected.
c). The last step would be to process this selected person/company post's ID and connect it with the newly created biography post when the form is submitted:
add_action('cred_save_data', 'custom_people_save_data_action',10,2);
function custom_people_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1234)
{
$post_from_gen_field = $_POST['people-field'];
if(!empty($post_from_gen_field)) {
toolset_connect_posts( 'people-biography_relationship_slug', $post_from_gen_field, $post_id );
}
}
}
Note: You'll replace 1234, with the actual ID of your post form and "people-biography_relationship_slug" with the slug of the relationship. You can learn more about the "cred_save_data" hook and the "toolset_connect_posts" function, from these links:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
The above code snippet too can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar