Skip Navigation

[Résolu] CRED – using a view to conditionally display parent posts in dropdown selector

This support ticket is created Il y a 9 années. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 16 réponses, has 3 voix.

Last updated by tony Il y a 8 années et 12 mois.

Assigned support staff: Luo Yang.

Auteur
Publications
#187844

Ana Couto kindly supplied some code that enabled me to filter parent posts selection lists using a view to modify the dropdown content.

Example: Requesting a boat

Post types are "Boats" "Members" and "Requests" with Requests linking to both Boats and Members, creating a many to many relationship.

In my example, I only want to allow members to request a "club boat", which is a subset of all of the Boats in the system. Using a View, I can create a list of just those boats that I want to be selectable.

Using a standard CRED form, when entering request details, a member would be able to select any boat in the system but by applying the View filter only the "club boats" show in the dropdown.

The code to do this is

==========

jQuery(document).ready(function($) {
  try {
    var $select = $('select[name="_wpcf_belongs_boat_id"]');
    var $list = $('#aux_boat_list') || $('<div id="aux_boat_list"></div>');
    if ($select.length) {
      var selected = $select.val();
      var $new_select = $select.clone(true, true);
      $new_select.children().remove();
      var $styled_list = $('#dk_container_' + $select.attr('id') + ' div.dk_options ul.dk_options_inner');
	  if ($styled_list.length) {
		var $new_styled_list = $styled_list.clone(true, true);
		$new_styled_list.children().remove();
	  }
	  if ($('option[value="-1"]', $select).length) {
		  $('option[value="-1"]', $select).appendTo($new_select);
		  if ($styled_list.length) {
			$('li a[data-dk-dropdown-value="-1"]', $styled_list).parent().appendTo($new_styled_list);
		  }
	  }
      $('.aux_id', $list).each(function(index, element) {
        var id = $(element).text() || 0;
        $('option[value="' + id + '"]', $select).appendTo($new_select);
		if ($styled_list.length) {
		  $('li a[data-dk-dropdown-value="' + id + '"]', $styled_list).parent().appendTo($new_styled_list);
		}
      });
      $new_select.val(selected);
      if ($styled_list.length) {
		$new_select.hide();
		$('li', $new_styled_list).removeClass('dk_option_current');
		$('li a[data-dk-dropdown-value="' + $new_select.val() + '"]', $new_styled_list).parent().addClass('dk_option_current');
		$('#dk_container_' + $select.attr('id')).data('dropkick').$select = $new_select;
        $styled_list.replaceWith($new_styled_list);
      }
      $select.replaceWith($new_select);
    }
  } catch(e) {
  }
});

==========

You can find the full tutorial here:
https://toolset.com/2013/10/conditional-display-of-parent-posts-in-cred/

Now, what I want to do is filter a dropdown selection where the parent/child relationship is the same post type. So for example, I have a post type called "Event" and I want to be able to link one event to a parent event but only certain events can be a parent.

I can create a view that identifies which events can be a parent but the code needs to be adjusted to make it work.

I think the solution is to change the following code somehow but I am unable to work out what the change needs to be!

jQuery(document).ready(function($) {
  try {
    var $select = $('select[name="_wpcf_belongs_boat_id"]');
    var $list = $('#aux_boat_list') || $('<div id="aux_boat_list"></div>');

Any help much appreciated.

Tony

Note:
I am using styled CRED forms.

#188574

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi tony,

Could you describe detail steps to duplicate same problem? or duplicate same problem with our discover-wp system,
Since it need a live website to debug, thanks
https://toolset.com/faq/how-and-why-to-create-a-test-site-in-discover-wp/

#188581

Hello Luoy

I will send you an email with access to our system.

Regards
Tony

#189168

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Tony, I checked the emails from your email: tony@***fordassociates.co.uk
There isn't email for this thread, but for other thread. could you send it again?
Also point out the problem CRED form and page URL, and include this thread URL for reference
Thanks

#189199

Hello Luoy

Details sent from my correct email account!

Many thanks
Tony

#190089

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Sorry, I still not get any new email from your email: tony@***fordassociates.co.uk
There isn't email for this thread
Please send it again, or duplicate same problem with our discover-wp system
https://toolset.com/faq/how-and-why-to-create-a-test-site-in-discover-wp/

#190120

Hello Luoy

No idea what is going wrong - sent again.

Regards
Tony

#190916

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I did not got the email yet, please make sure you are sending it to my email:
luo.y@onthegosystems.com
Thanks

#190932

I have no idea why my email is not getting through - just sent two more via different outgoing servers.
Tony

#191712

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Got the the email, and working on it, will feedback if there is any found

#192148

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Tony, Is this problem resolved? I asked help from the tutorial author, she says the problem is fixed, please let me know if you need more assistance

#192168

Hello Luo

No. The code works fine when the parent that you are trying to select is a different type and you can use "_wpcf_belongs_". In my example the parent is the same post type and the code needs some adjustment so it works on a single post type - event-1 > event-2.

When selecting a parent event I am filtering out events that cannot be used as a parent event.

#192590

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I forward you question to our developers, will feedback if there is any found

#193049

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I have put it into our to-do list, our developers will take care of it, and I will feedback if there is any news.

#193065

Thank you Luoy

The ability to modify drop down parent selections is a very useful facility in CRED. As I am sure you will appreciate, many databases retain historic data and as time goes by the selection box gets cluttered with old values that you do not want the user to select (but you do not want to remove the links). In addition, sometimes you don't want a user to select a value that may be "more than a month ahead". The code I have for modifying parent selections (different post type parent) works really well. If this can be built in to CRED and can be enhanced to cover the same post type as a parent, I think this would be a much used facility.

I look forward to some news at a future date.

Best Regards
Tony