### Tell us what you are trying to do?
I need to have the slug of a child post based on the content of the parent post slug.
The function I have defined is this:
add_filter("wp_unique_post_slug",
function ($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) {
merr_log("post_ID=" . $post_ID);
$rr_slug=$slug;
// custom slug only for 'pianta' cpt
if($post_type === 'pianta'){
// get the parent post ID then the slug to build the prefix
merr_log("getting parent post id then slug ...");
$parent_post_ID = toolset_get_related_post($post_ID, 'frutteto-pianta' );
$parent_obj = get_post($parent_post_ID);
$parent_slug = $parent_obj->post_name;
$rr_slug = $parent_slug . "-" . $slug;
}
merr_log("exiting with rr_slug=" . $rr_slug);
return $rr_slug;
}, 10, 6
);
As you can see, the child post slug is created by prefixing to it the parent post slug.
If I create the child post manually, using the standard wp interface, the function will works fine.
If I create the child post using the Toolset popup that allows you to create a new related post from within the parent post, an error occurs (debug.log output):
[08-Jun-2021 17:19:06 UTC] post_ID=0
[08-Jun-2021 17:19:06 UTC] PHP Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /var/www/vhosts/mensebio.it/merr.servizinoncomuni.it/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246
Stack trace:
#0 /var/www/vhosts/mensebio.it/merr.servizinoncomuni.it/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts-set_query_by_elements()
The error seems to happening because the function runs but did not receive the parent postID.
Any suggestion?
### What is the link to your site?
hidden link
Hello,
The filter hook "wp_unique_post_slug" triggers before relationship was setup.
In your case, I suggest you try the action hook wp_insert_post, like this:
add_action('wp_insert_post', 'set_pianta_slug_func', 10, 3);
function set_pianta_slug_func($post_ID, $post, $update) {
//merr_log("post_ID=" . $post_ID);
$slug = $post->post_name;
$post_type = $post->post_type;
// custom slug only for 'pianta' cpt
if($post_type === 'pianta'){
// get the parent post ID then the slug to build the prefix
//merr_log("getting parent post id then slug ...");
$parent_post_ID = toolset_get_related_post($post_ID, 'frutteto-pianta' );
if($parent_post_ID){
$parent_obj = get_post($parent_post_ID);
$parent_slug = $parent_obj->post_name;
$slug = $parent_slug . "-" . $slug;
$my_post = array(
'ID' => $post_ID,
'post_name' => $slug,
);
remove_action('wp_insert_post', 'set_pianta_slug_func');
wp_update_post( $my_post );
}
}
//merr_log("exiting with rr_slug=" . $rr_slug);
}
More help:
https://developer.wordpress.org/reference/hooks/wp_insert_post/
Fires once a post has been saved.
My issue is brilliantly resolved now. Thank you!