Skip Navigation

[Resolved] View as select not working

This support ticket is created 2 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 16 replies, has 3 voices.

Last updated by michielM 2 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2355311

Earlier I found the way to use a view to populate select options. Alas, with a new form it doesn't work anymore.

My setup:
- Template of a Student
has a: view of Student Locations Intermediary posts
each Student Location post has a: Edit form to fill in the Intermediary Post Field 'workshop'.
The Workshop has to be filled in by selecting a choice from a list generated by a View.

For the View of the available Workshop to be generated I need the Location Id, but I don't see in within the options under "Fields and Views" in the Form. How can I show the proper list of Workshops based on the Location that is connected to the Student Location post?

If you want, I can give access.

#2355339

Okay, I have made a View to retrieve the Location ID, but it still doesn't work:

          [cred_generic_field type='select' field='workshop-leerling-locatie' class="form-control"]
            {
            "required":1,
            "options":[ [wpv-view name="leerling-inschrijving-workshops-options" wpvrelatedto="[wpv-view name="locatie-id-van-locatie-leerling-post" wpvrelatedto="[wpv-post-id]"]" ] ]
            }
          [/cred_generic_field]

I'm guessing the quotes are getting confusing?

#2355425

Nigel
Supporter

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

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

Hi there

What does the output of the View on its own look like in the same context (in the form, but not inside the generic field)?

Is this right? You are nesting Views both of which use the wpvrelatedto attribute.

[wpv-view name="leerling-inschrijving-workshops-options" wpvrelatedto="[wpv-view name="locatie-id-van-locatie-leerling-post" wpvrelatedto="[wpv-post-id]"]" ]

As indicated in the generic select field settings, the output of the View (the outer View) needs to generate content in this format (i.e. JSON):

{"value": "value1", "label": "Label 1"}, {"value": "value2", "label": "Label 2", "default": true}

Is that what the View generates?

#2357007

Hi Nigel,

The problem is not the "leerling-inschrijving-workshops-options"-view (it works fine when I put a valid ID in the wpvrelatedto field), but the other one: locatie-id-van-locatie-leerling-post.

If I add the view in normal HTML, I get the proper ID of the post I'm looking for. No divs or other markup.

What is also interesting that the code I wrote in the previous reply doesn't work at all, no select field is even created within the form. However, if I put backslashes before the quotes in the wpvrelatedto field there is a select field with an error: View could not be found.

#2357033

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It looks like more issue about the nested shortcode and I'll have to check on your install.

Can you please share problem URL where you added the view's shortcode as well as admin access details let me see if I'm able to share any workaround or any other solution.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2357109

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As I understand, your goal is to get all the related post IDs based on the current post using the following view:

[wpv-view name='locatie-id-van-locatie-leerling-post' wpvrelatedto='[wpv-post-id]']

So I see the current post which we display on frontend: hidden link
- It has four related posts with relationship "Leerlingen Lokaties"

And once we get all those related post IDs based on the current post we display then you want to pass those IDs to fetch related post of post relationship "Lokaties Workshops" and display the workshops as options - correct?

#2357111

Yes, indeed!

#2357135

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

With the view you created there was multiple issues as when you filter with the post relationship as it return multiple values as well as issue with nested views shortcodes.

As a more efficient workaround that should not cause any issues even in future:

I've added the following shortcode to "Custom Code" section offered by Toolset with code snippet "toolset-custom-code":
=> hidden link

add_shortcode('get_generic_select_options', 'func_get_json_for_generic_select');
function func_get_json_for_generic_select() {	 
	global $post;
  
   $localities_ids = toolset_get_related_posts(
                                     $post->ID,
                                     'leerling-lokatie',
                                      'parent',
                                      999,
                                       0,
                                       array(),
                                      'post_id',
                                      'child'
                                      );
  if(!empty($localities_ids)) {
    	$workshop_ids = array();
    	foreach($localities_ids as $k=>$v):
    
    			$workshop_ids[] = toolset_get_related_posts(
                                     $v,
                                     'lokatie-workshop',
                                      'parent',
                                      999,
                                       0,
                                       array(),
                                      'post_id',
                                      'child'
                                      );
    		
    	endforeach;
  }
  
  $workshop_ids = join(",",array_merge(...array_values($workshop_ids)));

  
  $workshops = do_shortcode('[wpv-view name="leerling-inschrijving-workshops-options" ids="'.$workshop_ids.'"]');
  
	return trim($workshops);
}

The above snipped will find the related localities based on the current post and then based on the related localities of current post it will find all the related workshops IDs and we pass those found workshop IDs to view "leerling-inschrijving-workshops-options" where I've changed the query filter for that view to filter by post ID:
=> hidden link

Post ID filter
Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]

At last I've passed the above shortcode to the generic field:

<div class="form-group col-md-2">
			<label for="%%FORM_ID%%_workshop-leerling-locatie">[cred_i18n name='workshop-leerling-locatie-label']Workshop:[/cred_i18n]</label>
		</div>
		<div class="col-md-8">
            [cred_generic_field type='select' field='workshop-leerling-locatie' class="form-control"]
            {
            "required":1,
            "options":[ [get_generic_select_options] ]
            }
          [/cred_generic_field]
      </div>

I can see it displays the workshops with select dropdown: hidden link

Can you please confirm it works as expected.

#2357173

Hi Minesh,

Thanks for your time, but the select should only show the related Workshops of a certain Location. But I don't have time to look into it now. Hopefully later today.

#2358599

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

ok, please tell me by example for what record what posts (workshops) should be get displayed or you are welcome to adjust the code I shared according to your requirement.

#2358827

Hi Minesh,

It's exactly what you wrote before:

And once we get all those related post IDs based on the current post we display then you want to pass those IDs to fetch related post of post relationship "Lokaties Workshops" and display the workshops as options - correct?

I could probably change the code, but what I'm stuggling with is how to get the related Location ID of the current Student Leerling intermediary post in the loop. (I need it to retrieve the proper Workshops that are given at a certain Location.) That is still not shown in your example. Could you show me how to give the shortcode a parameter?

Myabe something like this:

[get_generic_select_options locationid="123"]

If this works, I still don't have the locationid. If that's not possible to retrieve, then I guess I need to work with the current Student Location id and find the connected Location ID in the code. Right?

Maybe some background knowledge might help:
I have multiple locations which all students have to attend. At each location there is a specific number of workshops. The students have to pick one from that list. So, in other words: a students needs to pick one workshop for each location. The relationship between the Locations and Workshops has already been made by an admin.

#2358847

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

For instance, if you can share that with the following learning:
- hidden link

With the above post and with the very first record "Groencollege" if you can tell me what workshop we should display and why that will be easy for me to understand.

#2358851

Here comes!

Groencollege:
- Agro en Groen, Grond en Infra
- Groene Ruimte – Hovenier – groene leefomgeving

Bouw:
-Schildersvakopleiding
-Bouw

Techniekhuis:
-Procestechniek
-HiTech

Mercedes / De Jager-Tolhoek:
-MotorVoertuigentechniek
-Logistiek&Maritiem

If you open one of the Lokaties, you'll see the related Workshops under "Lokaties Workshops".

#2358867

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

could you please check now: hidden link

I've adjusted the code added to the "Custom Code" section as given under:
=> hidden link

add_shortcode('get_generic_select_options', 'func_get_json_for_generic_select');
function func_get_json_for_generic_select() {	 
	global $post;
  
 $locid = do_shortcode("[wpv-post-id item='@leerling-lokatie.child']");
 
  /*
   $localities_ids = toolset_get_related_posts(
                                     $post->ID,
                                     'leerling-lokatie',
                                      'parent',
                                      999,
                                       0,
                                       array(),
                                      'post_id',
                                      'child'
                                      );
  */
  //if(!empty($localities_ids)) {
  
  if(!empty($locid)) {
    	$workshop_ids = array();
    	//foreach($localities_ids as $k=>$v):
    
    			$workshop_ids[] = toolset_get_related_posts(
                                     $locid,
                                     'lokatie-workshop',
                                      'parent',
                                      999,
                                       0,
                                       array(),
                                      'post_id',
                                      'child'
                                      );
    		
    	//endforeach;
  }
  
  $workshop_ids = join(",",array_merge(...array_values($workshop_ids)));

  
  $workshops = do_shortcode('[wpv-view name="leerling-inschrijving-workshops-options" ids="'.$workshop_ids.'"]');
  
	return trim($workshops);
}

Can you please confirm it works as expected.

#2358873

Thanks, Minesh! Works like a charm.