Skip Navigation

[Resolved] Create and Populate a Multiple Select Using other CPT

This support ticket is created 6 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.

Our next available supporter will start replying to tickets in about 1.74 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#911036

Hello,

The site is for Private tutors to offer their tutoring services. And we need to be able to have the Teachers choose which train lines and train stations they are available to work at.

We have a cpt for Train Lines (there are about 150). And child posts for the Stations on each train line (about 2000).

The goal is to allow users to be able to select the train lines they work on and the Stations along each line that they work at. They should be able to select multiple train lines and multiple stations along each train line.

So for example a teacher should be able to select:

Teacher x
*Train Line A
- station 1
- station 4
- station 5

* Train Line B
- station 2
- station 7
- station 11
- station 18

Teacher y
* Train Line B
- station 2
- station 7
- station 11
- station 18

*Train Line B
- station 23
- station 27
- station 21
- station 28

etc.

I’ve looked at various support topics but nothing seems to address something like this. Is there anyway I can do this with a Cred form?

Thank you

Tim

#911050

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-06-08 at 11.12.34.png

Hi Tim

We are working on an update to Views that would make this a little easier, but for now let me explain what's currently involved.

You can add a generic field to your Form and add the persist: 1 option to save the field to the database when submitted.

In the screenshot you can see me adding a generic multi-select field ("selections") where I am using a View to generate the dropdown options.

That inserts markup like so:

[cred_generic_field field='selections' type='multiselect' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="options-view"] ],
"persist":1
}
[/cred_generic_field]

Note how I manually added the persist option to save this field.

Now, the problem is that the output of Views includes wrapper markup which is required to make things like pagination and front-end filtering work.

We don't need any of that, we just need a View which generates output in the required JSON format.

That format needs to look like this:

"options":[
{"value":"1","label":"One"},
{"value":"2","label":"Two"},
{"value":"3","label":"Three"}
]

We are working on adding a "clean" output format for Views which will facilitate this, but in the meantime the only solution is to use a regex to strip out the surrounding wrapper.

So, having created a View to generate your options in the format shown above, you would need to add the following to your theme's functions.php file to "clean" the View output:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'tssupp_clean_view_output', 5, 2 );
function tssupp_clean_view_output( $out, $id ) {

	if ( $id == '375' ) { // Edit View ID
		$start = strpos( $out, '<!-- wpv-loop-start -->' );
		if ( 
		$start !== false
		&& strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
		) {
			$start = $start + strlen( '<!-- wpv-loop-start -->' );
			$out = substr( $out , $start );
			$end = strrpos( $out, '<!-- wpv-loop-end -->' );
			$out = substr( $out, 0, $end );
		}
	}

	return $out;
}