Skip Navigation

[Closed] pn the hook before delte i cannot get the post id

This support ticket is created 3 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by Christian Cox 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1646179

Hi ,
A have a one to many relationship with parretn "Album" and childs "Tracks"

i am using the
[cred_delete_post_link action='delete' message='' message_show='1' class='cred-refresh-after-delete'] ''
[/cred_delete_post_link] to create a button than deletes the album

i am trying to create a snipet that runs before the delete of albums in orerd to find all the childs of this album and delete them as well

function delete_album($post)
{
$id = $post->ID;
if(get_post_type( $id) == 'myalbum' ){

$relationship = 'album-track';
$param = '@' . $relationship . '_parent';
$child_posts = toolset_get_related_posts($id, $relationship, array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_object',
'role_to_return' => 'child',
));
... other code here
}
}

add_action( 'before_delete_post', 'delete_album', 12, 1 );

my problem is that ID is always empty an the $child_posts = toolset_get_related_posts return an error
PHP Notice: Trying to get property 'ID' of non-object

how get i get i fix this ?

#1646597

I managed to get the ID of the Album by using the $post instead the $post_id (that returns the id) in the function but now the
query to find all children : (Parrent = Album, Chldren = tracks)

$relationship = 'album-track';
$param = '@' . $relationship . '_parent';
$child_posts = toolset_get_related_posts($post, $relationship, array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_object',
'role_to_return' => 'child',
));

returns no children posts;

#1646915

Hi, can you try hard-coding the post ID as a test? I also added an empty args array for testing:

$relationship = 'album-track';
$child_posts = toolset_get_related_posts(12345, $relationship, array(
'query_by_role' => 'parent',
'limit' => 999,
'args' => [],
'return' => 'post_object',
'role_to_return' => 'child',
));

Change 12345 to the known ID of some parent album.

FYI - before_delete_post gives you access to the post ID, not the post object: https://developer.wordpress.org/reference/hooks/before_delete_post/

#1651281

HI,
I am using the same code in two different actions. one works one does not

1.// to add a new track custom post record -> works fine and returns all the childer
add_action('cred_save_data', 'add_new_track', 16, 2); -

function add_new_track($post_id, $form_data) {
(code...)
$relationship = 'album-track';
$param = '@' . $relationship . '_parent';
$parent_id = $_REQUEST[$param];
$child_posts = toolset_get_related_posts($id, $relationship, array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_object',
'role_to_return' => 'child',
));
$child_posts[] = get_post($post_id);
(code...)
}

2.// to validate the form Albums (in the above case used for Tracks which are the children of albums in one to many relationships) before save --> does not return the $parent_id

add_filter('cred_form_validate','func_check_unique',10,2);
function func_check_unique($error_fields, $form_data){
$id = get_the_ID();
$relationship = 'album-track';
$param = '@' . $relationship . '_parent';
$parent_id = $_REQUEST[$param];
(code...)
}

I have tried to use the same code on the before_save and also does not return the childer
add_action('before_delete_post', 'del_album', 20, 1);
function delete_album($post_id){
(code...)
$relationship = 'album-track';
$param = '@' . $relationship . '_parent';
$parent_id = $_REQUEST[$param];
$child_posts = toolset_get_related_posts($id, $relationship, array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_object',
'role_to_return' => 'child',
));
$child_posts[] = get_post($post_id);
(code...)
}

#1654387

2.// to validate the form Albums (in the above case used for Tracks which are the children of albums in one to many relationships) before save --> does not return the $parent_id
If I understand correctly, you are having trouble in the cred_form_validate script. Can you turn on error logs to debug this? If you're not familiar with error logs I can show you how to activate them temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

Now when you call error_log('foo') in PHP, it will write out debug information into a file called error_log.txt in your site's root directory. Add some logging in your validation callback so we can see what's going on here.

add_filter('cred_form_validate','func_check_unique',10,2);
function func_check_unique($error_fields, $form_data){
$id = get_the_ID();
error_log('id: ' . $id );
...

Which ID do you expect to see here? Which ID is actually written into the log?

The topic ‘[Closed] pn the hook before delte i cannot get the post id’ is closed to new replies.