Skip Navigation

[Resolved] Auto populate dropdown

This support ticket is created 7 years, 3 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
- 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: America/Toronto (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by AndreG3332 7 years, 2 months ago.

Assisted by: Meagan Hanes.

Author
Posts
#486653

Hi I I am building a CPT for one of our local eisteddfod competitions and there is one thing I need to do, but it is a bit above me and searches didn't give me any solutions.

The basic is I have to create a user form with cred with various fields, some of the fields are straight forward data entries but some of them need to be auto-populated with options using I assume "select". what I have done was to create 4 "custom posts" options where the auto-populated fields must be populated from. The reason why we had to go with this type of approach is that the volume of options is vast.

The one options "items:" are 185 items, Schools will account to about 70 - 80 entries, venues to about 50 venues. We need to make this so that it is easy for our clients to edit as everytime the eisteddfod is on the items change.

So to summarise, how do I achieve a dropdown that is populated from another post entries.

#486779

Hi there! Thanks for your question about how to create a CRED dropdown field that is automatically populated with your custom post types.

I'm happy to say that this is possible by creating a custom View that displays the data in the proper JSON format needed to combine with our Generic Field option.

1. Create a View that lists your Custom Post Type titles. Modify the Loop Output to produce valid JSON. An example of this can be found here: https://toolset.com/forums/topic/views-api-output-json-format-for-bootstrap-calendar/#post-323014

2. On your CRED form, Insert a Generic Field, choose "Select", and enter the shortcode to the View created above.

So long as your View is producing correct JSON, the CRED form's dropdown will match the output of the View!

Hope this helps!
Meagan

#486915
brakpan zoomed page deelnemers.png
brakpan full page deelnemers.png
brakpan json.png

HI Meagan,

Thx for your prompt reply. This is brand new stuff to me. I have tried to follow the instruction on the link and I do believe I have met with some success however the drop down is still empty.

Let me go through this and maybe you will be able to see where did I Fudge it up!

The code on the view

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-item index=1]
      {"value":"[types field='naam-van-skool'][/types]","label":"[types field='naam-van-skool']"}
       
      [wpv-item index=other],{"value":"[types field='naam-van-skool']","label":"[types field='naam-van-skool']"}
	</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]

Resulted in the page (screen print attached) I am not sure what a json output would look like, So I don't know if this is correct. From research, it does seem that I have the basics correct although not formated.

The next screen print is from the Edit Post Field Group - This is where the "select" field was inserted. I will include two screen prints one for the whole form and one zoomed in on the specific offending field. The link to the output URL for the view is
hidden link
and the shortcode used on the page and the "select" option is "[wpv-view name="json-view-skole"]"

Thx once again for your assistance.

#487067

Thanks for the information!

My colleague had a look at your site and noticed that if you view the source of the page, you'll see that there's a wrapper div being added to our View which is causing the issue.

To fix this, please add this function to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
    
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '999' ) { //Please adjust to your Views 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;
}

Note: please change the $id variable to be that of your View.
This function removes the unneeded div, resulting in only JSON being delivered. Your dropdown should work with this function added.

Let me know if you're encountering troubles after this!
Meagan

#487095

Hi Meagan,

OK this is a staging site so I quickly installed the theme i am going to use live which is generate press. I tried it without the function and with the function but no success. still empty drop down.

While on the topic, i am going to use about 4 dropdown fields, how do I specify multiple id's? With a ","? ie 22,25,36,14?

#487140

I can see the JSON is now valid, so the dropdown should be appearing in your CRED form.

I've taken a closer look at your screenshots and see that you are trying to add this to the Types field definitions rather than to a CRED form.

The advice I've given you was for populating a select dropdown in a CRED form with the output of a View. We will need to change our approach if your intention is for users to do this in the backend, so could you please clarify what your intention is exactly.

Thanks!
Meagan

#487456
field record brakpan.png

Hi Meagan,

That was such a "blond" moment that. Sorry. However, I am still not getting the results in a cred form either. Here is a url of a page with the cred form and as you can see the :"skool" field dropdown is blank.

hidden link

and i am attaching screen print of the select field also

Here is the code from the cred form if that may tell you anything.


[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='']

	<div class="cred-field cred-field-post_title">
		<label class="cred-label">
Deelnemer Name
</label>
		[cred_field field='post_title' post='deelnemer' value='' urlparam='']
	</div>

	<div class="cred-group cred-group-Deelnemers-Groep">
		<div class="cred-header"><h3>Deelnemers Groep</h3></div>
		<div class="cred-field cred-field-voornaam">
			<label class="cred-label">
Naam
</label>
			[cred_field field='voornaam' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-surname">
			<label class="cred-label">
Surname
</label>
			[cred_field field='surname' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-gender">
			<label class="cred-label">
Gender
</label>
			[cred_field field='gender' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-e-pos">
			<label class="cred-label">
E-pos
</label>
			[cred_field field='e-pos' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-kontak-no">
			<label class="cred-label">
Kontak nr
</label>
			[cred_field field='kontak-no' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-skool">
			<label class="cred-label">
Skool naam
</label>
			[cred_field field='skool' post='deelnemer' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-item">
			<label class="cred-label">
Item
</label>
			[cred_field field='item' post='deelnemer' value='' urlparam='']
		</div>
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='']

[/credform]

#487541

Thanks for the information.

I wasn't able to find the screenshot you mentioned was attached showing the dropdown, and when I went to hidden link I was met with a completely blank page, not even the CRED form without the field working.

In looking at your CRED form code, for the field Skool Nam (the one I believe you're trying to get the dropdown working for), I did not see the correct code used to call the Generic Field and include the View shortcode. This could be why it's not displaying.

Please follow these instructions to create a Generic Field:
On your CRED form, Insert a Generic Field, choose "Select", and enter the shortcode to the View which produces the JSON listing.

Let me know if that gets it working!
Meagan

#487791

Hi Meagan sorry that was to do with permissions, Would you mind to login and have a look. I have tried it and still get the same results. I will send you full details with the login details

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