Hi Kostas,
Thanks for sharing the admin area access details.
I've checked the code in your plugin's file and noticed that the function name that is being called with hooks, doesn't match the one defined for the code, which means that the code is never getting executed.
( screenshot: hidden link )
Also, the custom function doesn't restrict the code execution for a specific post type or a CRED form, which means it will be running unnecessarily resulting in a potential performance issue.
The hooks "save_post" and "cred_save_data" expect functions with a different number of parameters, therefore, you'll need to create two different functions for the admin area editing ("save_post") and the CRED form ("cred_save_data").
Example code with "save_post" hook:
function auto_get_children_from_parent( $post_id ) {
// if a specific post type
if ( get_post_type( $post_id ) == 'target' ) {
// get parent post from the relationship
$query_by_element = get_the_ID(); // ID of post to get relationship from
$relationship = 'target-collection-target'; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a child in this relation
$limit = 1; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'parent'; // We want parent.
$get_parent_result = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
if(!empty($get_parent_result[0]))
{
$parent_id = $get_parent_result[0];
// get child posts from the relationship
$query_by_element = $parent_id; // ID of post to get relationship from
$relationship = 'target-collection-target'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 100; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
$get_child_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
}
}
}
add_action( 'save_post', 'auto_get_children_from_parent', 99 );
Example code with "cred_save_data" hook:
function auto_get_children_from_parent_form( $post_id, $form_data ) {
// if a specific form
if ($form_data['id']==123) {
// get parent post from the relationship
$query_by_element = $post_id; // ID of post to get relationship from
$relationship = 'target-collection-target'; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a child in this relation
$limit = 1; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'parent'; // We want parent.
$get_parent_result = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
if(!empty($get_parent_result[0]))
{
$parent_id = $get_parent_result[0];
// get child posts from the relationship
$query_by_element = $parent_id; // ID of post to get relationship from
$relationship = 'target-collection-target'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 100; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
$get_child_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
}
}
}
add_action('cred_save_data', 'auto_get_children_from_parent_form', 10, 2);
Note: Please replace "123" with the actual CRED form ID, for which you'd like to execute this code.
For both functions, you'll get the IDs of all child posts of the parent, in relation with the current post, into the array "$get_child_results".
Tip: To debug the custom code step-by-step, you'll find this guide useful:
hidden link
regards,
Waqar