Skip Navigation

[Resolved] creating post relationships automatically without using relationship field

This support ticket is created 5 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 19 replies, has 2 voices.

Last updated by davelow 5 years, 12 months ago.

Assisted by: Minesh.

Author
Posts
#1182329

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.

#1182359

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 🙂

#1182388

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

#1182393

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;
}
#1182394

My issue is resolved now. Thank you!