Skip Navigation

[Resolved] create view which can be reused to populate several dropdown lists

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by davidm-13 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#922106
ListName1.jPG
ListName2.JPG
ListName3.JPG
ListName4.JPG
ListName5.JPG
ListName6.JPG

Hi,
I want to create one view that i can reuse to populate several dropdown select lists.
Screenshots ListName1.jpg - ListName4.jpg show what I've done so far to create the view.
ListName5.jpg - ListName6.jpg show the shortcode in the front end.
Please could you tell me how to setup the loop to receive the custom field to search by from the front end and also if you see from the screenshots any corrections I need to make.

Thanks
David

#922140

Hi, here's what I've done over the weekend:
the Loop:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
			<option>[wpv-post-body view_template="Loop item in ListName"]</option>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

and in "Loop item in ListName":

[types field=[wpv-attribute name="listfield"]][/types]

but it's still not working

#922248

The main problem here is that Views will create extra markup and HTML structure around the results, which will break the markup structure of a select field and its nested options. I'm not able to tell what you're trying to accomplish with this select field. If it's in a Toolset Form, then you may be able to use generic fields to accomplish something similar. Several other tickets in the forum discuss an approach that can be used to generate options for a select field in a Form using Views, similar to what you're describing here.

Reference ticket:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

The main things you should know about this approach are:
- It's not recommended to use Views as a data provider. Other Users have had luck with this approach, but I don't recommend it because it's not the intended purpose of Views.
- Pagination and parametric search features cannot be applied to this View.
- Future features may not be compatible with this approach.

#922253

Hi Christian, thanks for your response.
The select field is a dropdown list which is just in a <div> on a page - no forms.
I understood that wpv-attribute could be used to pass parameters from page to view - is this not correct?
What is the best way to populate a <select></select> list from a custom field? would it be simplest to create a separate view for each select list?
Thanks for your help
David

#922686

I understood that wpv-attribute could be used to pass parameters from page to view - is this not correct?
Yes that's correct, inside the Loop Output editor you can use the wpv-attribute shortcode to access the shortcode attributes applied in the View's shortcode.

What is the best way to populate a <select></select> list from a custom field?
I assume you mean you want to create a select field that has one option for each possible value of the custom field. This isn't really something Views is designed to do. Views is designed to loop over posts, or users, or taxonomy terms, but not options in a custom field. To loop over items with Views, you would have to convert these custom field options to a custom taxonomy as terms, or to a custom post type as posts.

Forms will allow you to create generic input fields like this, or even generate them automatically based on the possible values of some custom field. In theory you could create a Form that includes input fields for each custom field, remove the "submit" button, and add your own custom JavaScript as needed.

#922730

I understand from what you say my best option is to change my custom fields to taxonomies even though they're not categories but just terms - can't really understand why I can do this with taxonomies but not with custom fields, but thanks for the explanation. Guess I'll start again and reload the custom fields as taxonomies.

#923128

I understand from what you say my best option is to change my custom fields to taxonomies even though they're not categories but just terms
I'm not sure what you're trying to accomplish other than show a select field with an option for each possible value of the custom field, so I'm not able to say what your best option is just yet. These are just a couple of options that came to mind. If I wanted to show this select field on my site, I would use a Form. If you need to accomplish some other goal with this select field, please describe it in more detail, and explain why Forms are not appropriate. I may be able to come up with a better solution that doesn't require recreating all your custom field options as terms and reapplying them to all your posts.

Can't really understand why I can do this with taxonomies but not with custom fields
It's because Views content options are limited to posts (all post types), Users, and taxonomy terms. Custom field options are not included. If you'd like to suggest an improvement to Views that will allow you to loop over the options in a custom field, feel free to submit a new ticket using the "Suggest an Improvement" option here in the forums. Our developers will evaluate it and decide if they want to implement it.

#923204

Hi Christian, appreciate your help.
I'm setting up a website for medicinal plants of the Middle East, there are 2 post types Plant and Complaints Treated.
The fields for Plant posts that I want to create the dropdown lists for are botanical-name, english-name, arabic-name and hebrew-name selecting a name from any of these lists will link to the Complaints Treated page for that species - these are all custom fields 'cause I wasn't sure how to create the hierarchical structure of the taxonomy, there should probably be a main category like species and the names would be sub-categories of species, or would botanical-name be the main category and the english, hebrew and arabic names be sub categories? Also not sure how to import that structure in a csv file. So that's why they're custom fields although logically I understand they should be taxonomy fields.
I'm not sure what you mean by using a form - is that a Toolset CRED form? I thought these where for inputting data.
Appreciate your thoughts about which way is best and how I can achieve it.

Thanks
David

#923661

I'm not sure what you mean by using a form - is that a Toolset CRED form?
Okay yes, I'm talking about using a Toolset Form. The plugin was called Toolset CRED before version 2.0. Forms are usually used for inputting data to create a post or User, or edit a post or User. However in your case, you could apply custom JavaScript to the input fields generated by a Form to trigger navigation. Remove the submit button from the Form and you have a custom navigation system. Since the User never submits the Form, nothing is created or edited. You would have to write custom JavaScript anyway to handle navigation when the User changes one of the select fields. This way Toolset Forms generates those fields for you automatically.

You'll need to figure out how to create links to the correct Complaints Treated post URLs based on the custom field values. If the custom field values are the same as the Complaints Treated post slugs, then it's easy to link to those posts in JavaScript. For example, let's say when someone selects the English name field "Peppermint oil", you want to redirect the User to a Complaint Treated post with the slug of "peppermint-oil". So your custom field value for the option "Peppermint oil" should be "peppermint-oil".

<option value="peppermint-oil">Peppermint Oil</option>

Then in JavaScript you can redirect to the correct post using that slug whenever the English name field is changed:

jQuery(document).ready(function(){
  jQuery('select[name="wpcf-english-name"]').change(function(e){
    var slug= jQuery('select[name="wpcf-english-name"]').find('option:selected').val();
    window.location.href = "/complaint-treated/" + slug;
  });
});

Replace "wpcf-english-name" with the slug of your custom field, and be sure to include the "wpcf-" prefix.

#924427

Hi Christian,
Thanks for your time and patience.
In the end I changed the custom fields to taxonomies - 'cause they're much more user friendly and easily accessible - and created the following view loop for each dropdown list:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
			<option value="[wpv-post-url]">
				[wpv-taxonomy-title]
			</option>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Thanks again
David