Skip Navigation

[Resolved] accessing parent post within wp_unique_post_slug filter during new post creation

This thread is resolved. Here is a description of the problem and solution.

Problem:

Setup the child post slug with parent post slug.

Solution:

You can try the action hook wp_insert_post, like this:

https://toolset.com/forums/topic/accessing-parent-post-within-wp_unique_post_slug-filter-during-new-post-creation/#post-2082425

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/wp_insert_post/

This support ticket is created 3 years, 5 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by tipinoncomuni 3 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2082073

### 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

#2082425

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.

#2083165

My issue is brilliantly resolved now. Thank you!