Skip Navigation

[Resolved] Filter Select options in CRED Relationship Field

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 emilH 1 year, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2618719

Tell us what you are trying to do?
I have a post edit form with a Relationship Field where I would like to filter the options that are shown to the user.

Is there any documentation that you are following?
I found this forum post useful, but not sure how to replicate it to my scenario: https://toolset.com/forums/topic/select-posts-with-certain-term-in-relationship-dropdown-in-cred-form/

Admin login link available upon request.

I have three custom post types: Manufacturers, Robots, Case Stories

They have the following relationships:
Manufacturer is parent of Robot (one to many)
Manufacturer is parent of Cases (one to many)
Robot is parent of Cases (one to many)

I use the New/Edit Post Form to let users add new cases.

Each user belong to a specific manufacturer, the manufacturer ID is stored in a User Custom Field.

What I want to achieve is to have a select box on the New/Edit post form for "Case stories" that let the user select between different "Robots" but only select robots who are children of a specific manufacturer given by the ID stored in the Users Custom Field.

(I cannot use the Author = Current User option as each manufacturer has multiple users hence the author is not consistent.)

#2618909

Hi,

Thank you for contacting us and I'd be happy to assist.

Based on the solution that Minesh provided in the other support thread, you'll need to update the code, so that:

- it gets the custom user field value for the target manufacturer's ID
- next from that manufacturer's ID, it gets the related Robots posts, using the 'toolset_get_related_posts' function
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )
- and lastly, sets the query for the relationship filter to only those related Robots.

Here is what that code will look like:


function filter_parent_post_select2_by_manufacturer( $query ){
	// check for specific action
	if ( (!empty($_REQUEST['action'])) && ($_REQUEST['action'] == 'select2_potential_relationship_parents') && ( $_REQUEST['slug'] == 'robot-case' ) ) {
		// get user custom field value
		$manufacturer_id = types_render_usermeta( 'manufacturer-id', array('user_current' => true) );
		// if user custom field value exists
		if( !empty($manufacturer_id)) {
			// get related robots
			$get_related_robots = toolset_get_related_posts( $manufacturer_id, 'manufacturer-robot', 'parent', 99999, 0, array(), 'post_id', 'child' );
			// if related robots exist, set query to only include them
			if(!empty($get_related_robots)) {
				$query->set( 'post__in', $get_related_robots );
			}
		}
	}
}
add_action( 'pre_get_posts', 'filter_parent_post_select2_by_manufacturer', 101, 1 );

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 the active theme's "functions.php" file.

Note: You'll replace:

- robot-case: with the actual relationship slug of Robot Case relationship
- manufacturer-id: with the actual slug of your user custom field
- manufacturer-robot: with the actual slug of your Manufacturer Robot relationship

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

#2619079

It worked perfectly! Thank you so much!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.