Show only own posts in CRED parent post select dropdown

We can use the standard WordPress pre_get_posts hook to modify the query that retrieves the list of parent posts.

To limit its scope, edit the following code to change the ID of the page where the CRED child post form is inserted and the slug of the parent post type.

/**
 * Limit CRED parent post selector to own posts
 */
function filter_parent_posts( $query ){

	$target_page = 102; // ID of page with CRED form, or string of page slug
	$parent_post_type = "thing"; // slug of parent post type

	if ( is_page( $target_page ) && $query->get( 'post_type') == $parent_post_type ) {

		$current_user = get_current_user_id();

		$query->set( 'author', $current_user );

	}
}
add_action( 'pre_get_posts', 'filter_parent_posts' );

Note that when there are more than 15 possible parent posts to choose from the select2 JS library is added to the parent select dropdown which will break the above code. You can disable the select2 library for the parent selector by adding the attribute use_select2=”never” to the cred_field shortcode used to insert the parent selector in the form.

I produced a slightly more complex test that should also work on CRED edit forms and when select2 is enabled and the query takes place via ajax. This code is by way of illustration only, and depending on the specifics of the site the tests may or may not target the relevant query accurately enough.

/**
 * Limit CRED parent post selector to own posts
 */
function filter_parent_posts( $query ){
 
    $target_page = 102; // ID of page with CRED form, or string of page slug
    $parent_post_type = "thing"; // slug of parent post type
    $child_post_type = "thingy"; // slug of child post type
 
    if ( ( is_page( $target_page ) || is_singular( $child_post_type ) || ( defined('DOING_AJAX') && DOING_AJAX ) ) && $query->get( 'post_type') == $parent_post_type ) {
 
        $current_user = get_current_user_id();
 
        $query->set( 'author', $current_user );
    }
}
add_action( 'pre_get_posts', 'filter_parent_posts' );

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.

Display only parent items of the current user in CRED parent picker

By default CRED parent Picker lets you choose from all available parent Posts.
This snippet lets you populate the picker with only the parent posts of the current logged in user

This is how the CRED parent Picker field HTML has to look:

<div class="cred-group cred-group-parents">
  <div class="cred-field cred-field-_wpcf_belongs_page_id">//change "page" to your parent post slug
    <label class="cred-label">
      Choose Parent
    </label>
    <input type="hidden" id="parents_id" value="[get-parents]" /> This is only for test display: [get-parents]
      [cred_field field='_wpcf_belongs_page_id' value='']//change "page" to your Parent Post Slug
  </div>
</div>

This is the JS that has to be inserted to the CRED JS Editor

jQuery('document').ready(function(){
   
var post_parents = jQuery('#parents_id').val();
   
var arr = post_parents.split(',');
     
  jQuery("[name=_wpcf_belongs_page_id] > option").each(function() { //change "page" to your parent post slug
       
    var option_val = jQuery(this).val();
       
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
     
});

And this is the function to put into functions.php of your Theme

function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'page', 'posts_per_page' => '-1','author' => $current_user->ID,); //change page to your parent post slug 
    $author_posts = new WP_Query($author_query);
    $parent_ids = "";
    while($author_posts->have_posts()) : $author_posts->the_post();
        $parent_ids .= get_the_ID() .",";
    endwhile;
   
    return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

It is recommended to use Views with this and register the ShortCode in Views > Compatibility

Delete child posts as soon as its parent is deleted

Deletes all Child Posts as soon a Parent Post is deleted. Always make a backup first, mainly when you are running DELETE queries.

add_action( 'delete_post', 'wpv_delete_child_posts', 10 );

function wpv_delete_child_posts( $pid ) {
    global $wpdb;

    $ids = $wpdb->get_results( "SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wpcf_belongs_company_id' AND meta_value = '".$pid."'" );
 	
    if(isset($ids)){
   foreach ($ids as $key) {
   	wp_delete_post($key->post_id, true);
   }
}

    return true;
}

Limit a ‘n’ of posts users can submit with CRED

Limit the number of featured posts which user can submit. This Code limit to 1 featured post for each user.
The “wpcf-mls” is the custom field which define if the post is featured or not. It should store 1 for featured.

This hook will be triggered as soon as this custom field sent “1”.
Then check how many featured posts the current user has. Limit this number to “1”.

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data){
global $wpdb; 
list($fields,$errors)=$field_data;
   if ($form_data['id']==241){	
      if ($fields['wpcf-mls']['value'] == 1){
         $results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->posts.' post, '.$wpdb->postmeta.' meta WHERE post.ID = meta.post_id and post.post_status = "publish" and meta.meta_key = "wpcf-mls" and meta.meta_value = "1" AND post.post_author = "'.get_current_user_id().'"', OBJECT );
         if (count($results) > 1){
            $errors['mls']='Your limit for featured posts is 1';
         }    
      }
      return array($fields,$errors);
   }
}