[+] Filter post title options for the Connect Existing select2.js field in the post relationships panel of the post editor screen

In the post editor screen, a post relationships panel can be used to “Connect Existing” posts in a post relationship. Users must select from existing post titles using a select2.js custom select field. If you have several possible posts with similar names, it might not be obvious which post you should select. You might want to add some additional identifying information, like a custom field value or a related post title from another post relationship. You can use the posts_results hook to filter those post titles. The following code snippet shows how you might accomplish this.

/* --------------------------------------------------------
 * FILTER POST TITLES IN CONNECT EXISTING SELECT2.JS FIELD 
 * IN POST RELATIONSHIPS PANEL IN POST EDITOR SCREEN
 * Use posts_results hook to customize the post titles shown as options in the select2.js field 
 * displayed when using the Connect Existing feature in post relationships panel of post editor screen
 * @$posts: array of post objects to be displayed in select2.js field
 * @$query: query that was used to fetch $posts results
 */
function ts_filter_connect_existing_titles( $posts, $query ){
  $post_types = ['band', 'event'];  // edit the slug(s) of post type(s) to modify
  $rel_slugs = ['band-event'];      // edit the slug(s) of post relationship(s) where you want to modify titles

  // !! - You probably should not edit anything in these conditionals. skip to the foreach code to manipulate titles - !!
  
  // We only want to run this if User is searching for related content for specific relationships using the Connect Existing dialog
  if ( defined('DOING_AJAX')
   && isset($_REQUEST['action'])
   && $_REQUEST['action']=='types_related_content_action'
   && isset($_REQUEST['relationship_slug'])
   && in_array($_REQUEST['relationship_slug'], $rel_slugs)
   && isset($query->query['post_type'][0])
   && in_array($query->query['post_type'][0], $post_types)
  ) {
    foreach($posts as $post){

      // this loops over each WP Post object that will be included in select2.js options
      // modify the titles of each post here as desired
      $post->post_title = 'Test-' . $post->post_title;

    }
  }
  return $posts;
}
add_action( 'posts_results', 'ts_filter_connect_existing_titles', 99, 2 );

You can adjust the post type slugs band and event, as well as the post relationship slug band-event to match your content. This code will manipulate the titles of posts in any of the post types included in the $post_types array, in the Connect Existing dialog available for any of the post relationships included in the $rel_slugs array. Modify the post titles however you’d like in the foreach loop. Note that this does not extend search capabilities – i.e. you cannot search for the modified title. The modification only applies to the title displayed in the select2.js field options.

Combine two fields / Merge two fields

I have a CPT and some Custom fields in it. Two of the fields are wpcf-first-name (text) and wpcf-last-name (text). I have a 3rd field Fullname (text) which I want to be auto-filled with fname+lname. How it can be done?

Solution:
Please add below JS code in your site, you can use the Simple Custom CSS and JS plugin for this: https://wordpress.org/plugins/custom-css-js/


$('input[name="wpcf[firstname]"], input[name="wpcf[lastname]"]').on('blur', function(e) {
var first_name = $('input[name="wpcf[firstname]"]').val();
var last_name = $('input[name="wpcf[lastname]"]').val();
$('input[name="wpcf[fullname]"]').val(first_name + ' ' + last_name);
});

Screenshot here.

Populate select field with Fields from another CPT

To Get this to work you will need to replace the Text “Team Leader” with the name of the select field you wish to populate. Everything else is a Basic wordpress Query


add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Team leader':
            $options = array();
			$args = array(
                'post_type'        => 'team-leader',
                'post_status'      => 'publish',
				'meta_query' => array(
        							array(
							            'key'     => 'wpcf-active',
							            'value'   => 'Yes',
							            'compare' => '=',
        								),
									),
								);
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $first_name = get_post_meta($post->ID,'wpcf-first_name');
                $last_name = get_post_meta($post->ID,'wpcf-tmsurname');
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $first_name[0]." ".$last_name[0],
                     );
                }
               
                break;
}
    return $options;
}

Evaluating types field of a parent post inside a CRED form

Problem:

shortcode is not designed to understand the “future parent” post ID passed by CRED, but from the users point of view it’s a usability issue or even a bug.

Solution:

We can use a custom shortcde as a workaround for this.

Add to your functions.php of your theme:

function toolset_support_get_field_of_job( $field = 'wpcf-question-one' ) {
  if( ! isset( $_GET['parent_position_id'] ) ) {
    // no parent position id
    return '';
  }

  $question = get_post_meta( $_GET['parent_position_id'], $field, true );

  if( ! $question || empty( $question ) ) {
    // no valid value set in database
    return '';
  }

  // return the question
    return $question;
  }

  function shortcode_toolset_support_get_field_of_job( $atts ) {
    $atts = shortcode_atts( array(
                            'field' => 'wpcf-question-one'
                            ), 
            $atts );

    return toolset_support_get_field_of_job( $atts['field'] );
  }

add_shortcode( 'toolset_support_get_field_of_job', 'shortcode_toolset_support_get_field_of_job' );

 

Note: In your first post you said the link to the form looks like this: https://awesomesite.whatever/position-application/?parent_position_id=150

So I took ‘parent_position_id’ as key. If the real key differs you have to replace it in the above code.

Now go to Toolset -> Settings -> Front-end content and scroll down to the Third-party shortcode arguments section. Add the shortcode toolset_support_get_field_of_job there.

—-

Open you CRED From and you can add your answer/questions like this:

 

This way the fields are only shown if a question is added and the answers are stored correctly.