Skip Navigation

[Resolved] Automatically selecting the parent post in the form

This support ticket is created 4 years, 2 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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by katjaL 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1765939

Hi there,
I'm creating a site where logged in user can make an artist profile (custom post type "taiteilija") and then he can create some art (custom post type "work", child of "taiteilija") for this artist. I know how to create child post form link from parent page. But i need to create child post form which automatically sets the parent regardless where the user enters the page. The parent can be shown easily in dropdown select but I can't figure out how to make it default value (and then hide the field altogether).

And at the moment the form is in wizard mode.

#1767683
form-default-value-field.png

Hi,

Thank you for waiting, while I performed some tests on my website.

To automatically select the "artist profile" post option where the current user is the author, in the form to add a new child post type, you can follow these steps:

1. First, you'll need to register a custom shortcode that can get the current user's "artist profile" post ID.


add_shortcode('return_current_user_profile_id', 'return_current_user_profile_id_func');
function return_current_user_profile_id_func() {

	$current_user_id = do_shortcode('[wpv-current-user info="id"]');

	$args = array(
		'post_type'        => 'user-profile',
		'posts_per_page'   => 1,
		'post_status'      => 'publish',
		'author'           => $current_user_id,
		'fields'           => 'ids',
		);

	$posts_array = get_posts( $args );

	if(!empty($posts_array)) {
		return $posts_array['0'];
	}
	
}

Note: You'll replace "user-profile" with the actual slug of your website's "taiteilija" post type.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

2. Next, in the form to add new child posts, you can include this new shortcode "[return_current_user_profile_id]" in the "Field default value" option for the parent post field.
( example screenshot attached )

As a result, when the form will load on any page, it will have the current user's artist profile post automatically selected as the parent post.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1767693

This was a great and elegant solution, thank you Waqar!