coming back to this.. i understand linking them up via the relationship field at the front end would be dynamic, however, i cannot find a way to make it mandatory.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Ok - so, I've added following code to "Custom Code" section of Toolset:
=> hidden link
// fill Breed custom field select options dynamically
add_filter( 'wpt_field_options', 'func_fill_select_breeds', 10, 2);
function func_fill_select_breeds( $options, $title, $type ){
switch( $title ){
case 'Breed':
$options = array();
$args = array(
'post_type' => 'breeds',
'post_status' => 'publish',
'numberposts' => -1,
);
$ps_array = get_posts( $args );
foreach ($ps_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}
//Custom Post title from Custom Field and connect post
add_action('cred_save_data', 'func_custom_title_and_connect_post',10,2);
function func_custom_title_and_connect_post($post_id, $form_data){
// if form "Join the Community" and name field is populated
if ($form_data['id']==2364 && isset($_POST["wpcf-name"])) {
//set title of post to value of "name" field
$title = $_POST["wpcf-name"];
$slug = sanitize_title($title);
$my_post = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $slug,
);
// Update the post into the database
wp_update_post( $my_post );
if (isset($_POST['wpcf-breed'])){
$parent_id = $_POST['wpcf-breed'];
$parent = toolset_connect_posts('breed-dog', $parent_id, $post_id );
}
}
}
I've created one test post "toolset-test" and I can see its connected successfully to selected parent:
=> hidden link
I hope all issues resolved here and happy weekend 🙂
wonderful Minesh.
question:
I'll be expanding what we did for Breeds, to Vets.
So I'll accordingly add the codes?
case 'Vets':
$options = array();
$args = array(
'post_type' => 'Vets',
'post_status' => 'publish',
'numberposts' => -1,
);
$ps_array = get_posts( $args );
foreach ($ps_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
I am thinking I'll need 'Vets' to be a CPT by itself, because the original intent was to have Groomers, Trainers etc all under one type (Directory) which is the setup now. Am I correct?
thanks,
Dave
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
I've added the comments below:
add_filter( 'wpt_field_options', 'func_fill_select_breeds', 10, 2);
function func_fill_select_breeds( $options, $title, $type ){
switch( $title ){
case 'Breed': // this should be your custom field label
$options = array();
$args = array(
'post_type' => 'breeds', // this should be you post type slug
'post_status' => 'publish',
'numberposts' => -1,
);
$ps_array = get_posts( $args );
foreach ($ps_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}
My issue is resolved now. Thank you!