Hi George,
Thank you for waiting.
I was able to make this work using the following steps:
1. I added a post reference type field "Post Book" (slug "post-book") with the "Posts" post type and set it to select the post from the "Books" post type.
I also added a single line type custom field "Book Line" (slug "book-line") with the "Books" post type.
2. First, I registered a custom shortcode that generates the list of options for a generic field, from all the published "Books" post type. The label of these options would include both, the book post titles and the "Book Line" custom field value:
add_shortcode('get_custom_prf_options', 'get_custom_prf_options_fn');
function get_custom_prf_options_fn() {
$args = array(
'post_type' => 'book',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
$output = '';
foreach ($posts_array as $post) {
$post_field_value = types_render_field( "book-line", array( "item" => $post->ID ) );
if(!empty($post_field_value)) {
$output .= '{"value": "'.$post->ID.'", "label": "'.$post->post_title.' - '.$post_field_value.'"},';
}
else
{
$output .= '{"value": "'.$post->ID.'", "label": "'.$post->post_title.'"},';
}
}
return rtrim($output, ',');
}
Note: Please replace post type and custom field slugs in this code snippet, as per your website.
2. Next, I add a new post form to add "Posts" and in it included a select type generic field, instead of the post reference field "Post Book".
This generic field was set with slug "gen-select-field" with the options coming from the newly registered custom shortcode [get_custom_prf_options].
( as shown in the attached "screenshot-1")
3. In the form's "JS editor", I included some custom script to make this generic field, use select2 dropdown:
jQuery(document).ready(function() {
jQuery('select[name="gen-select-field"]').toolset_select2();
});
As a result, this generic field will show the dynamic list of options coming from the books post type.
4. The last step would be to process this selected book post's ID and save it in the post reference field "Post Book", in the newly created post, when the form is submitted:
add_action('cred_save_data', 'custom_save_data_action',10,2);
function custom_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1234)
{
$post_from_gen_field = $_POST['gen-select-field'];
if(!empty($post_from_gen_field)) {
toolset_connect_posts( 'post-book', $post_from_gen_field, $post_id );
}
}
}
Note: You'll replace 1234, with the actual ID of your post form. 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
I hope this helps and please let me know if any step/point is not clear.
Important: the custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar