Home › Toolset Professional Support › [Resolved] Populate a generic field (select) with results from a View
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 14 replies, has 2 voices.
Last updated by Bobby339 2 years, 5 months ago.
Assisted by: Minesh.
Hello I am trying to populate a generic field (select) with results from a View and I can't figure it out. Once I put the shortcode in the post form, just the data appears but no dropdown. See screenshot names problem.png.
I tried following along with these two resolved tickets but could not get it to work:
https://toolset.com/forums/topic/dynamically-populate-*select-field-2/#post-525640
To see the problem:
hidden link and type 4321 in the box and click the Verify button.
In my Post Form, I have this code in the generic select field:
[cred_generic_field type='select' field='wpcf_slexperience']
{
"required":1,
"default":[],
"persist":1,
"options":[[wpv-view name="smartlinkchecklist" user="[wpv-user field='ID']]
}
[/cred_generic_field]
I'm also including a screenshot of that (form.png)
The View (smartlinkchecklist) that it is referencing uses the logged in users ID to populate just that particular users "Experiences". The loop code looks like this:
{"value":"[wpv-post-id]","label": "[types field="exname"][/types]"},
I'm also including a screenshot of this code (view.png)
Can you help me make this work?
Hello. Thank you for contacting the Toolset support.
What if you try to use the following generic field code:
[cred_generic_field type='select' field='wpcf_slexperience'] { "required":1, "default":[], "persist":1, "options":[ [wpv-view name="smartlinkchecklist" user="[wpv-user field='ID'] ] } [/cred_generic_field]
do you see it working, if yes, the different is that we added the space before and after the view shortcode you added as options attribute source as you can see above.
I've added the generic field to your form as given under:
[cred_generic_field type='select' field='wpcf_slexperience'] { "required":1, "default":[], "persist":1, "options":[ [wpv-view name='smartlinkchecklist' user="[wpv-user field='ID']"] ]
And, with the view you created, I've set the view's Loop Editor section as given under:
=> hidden link
<wpv-loop> [wpv-item index=other] {"value":"[wpv-post-id]","label": "[types field="exname"][/types]"}, [wpv-item index=last] {"value":"[wpv-post-id]","label": "[types field="exname"][/types]"} </wpv-loop>
And I've also checked the checkbox "Disable the wrapping DIV around the View" that is available under the "loop editor" section.
To test I've to change the one of post's "owner id" field to 3 as the user you created for me "toolsetsupport" has id 3.
=> hidden link
Could you please check now if its works as expected.
OMG you fixed it! Thank you so much! It's amazing what forgetting one little " can do! Thanks for fixing the loop as well! AWESOME!
I tried removing (what I thought was) an extra
[wpv-view name='smartlinkchecklist' user="[wpv-user field='ID']"]
but when I did, it stopped working again?
Can you login again and tell me how I messed this up?
🙁 sorry
It seems the shortcode attribute with view with generic field is not working as expected:
[wpv-view name='smartlinkchecklist' user="[wpv-user field='ID']"]
So, I changed the generic field shortcode as given under:
[cred_generic_field type='select' field='wpcf-slexperience'] { "required":1, "default":[], "persist":1, "options":[ [wpv-view name='smartlinkchecklist'] ] } [/cred_generic_field]
And with your view, I've changed the query filter as given under:
=> hidden link
Custom field filter: Select items with field: ownerid is a string equal to 0
And within the "Custom code" section within the code snippet "smartlinkcheck1script" I've added the following filter code:
=> hidden link
function func_filter_by_shortcode_attribute( $query, $view_settings, $view_id ){ global $current_user; $target_views = array( 39 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { $query['meta_query'][0]['value'] = $current_user->ID; } return $query; } add_filter( 'wpv_filter_query', 'func_filter_by_shortcode_attribute', 10, 3 );
I can see its working as expected:
=> hidden link
Thank you for looking into that but it has gotten worse 🙁
Now when I visit another SmartLink URL, like this one: hidden link it is all white HOWEVER when I deactivate the smartlinkcheck1script it comes back BUT it is already on Step 2 (completely bypassing the PIN check.
See attached screenshots (named appropriately).
Can you get the Step 1 PIN page back AND the dropdown of experiences on Page 2?
Well - I removed the code I've added and it still does not display the SmartLink URL:hidden link
function func_filter_by_shortcode_attribute( $query, $view_settings, $view_id ){ global $current_user; $target_views = array( 39 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { $query['meta_query'][0]['value'] = $current_user->ID; } return $query; } add_filter( 'wpv_filter_query', 'func_filter_by_shortcode_attribute', 10, 3 );
It seems the issue is the code that is previously added to the code snippet "smartlinkcheck1script ":
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 19 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { // is there a filter set? if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 99, 3 )
As you acknowledged that if you delete the above code or deactivate the code snippet "smartlinkcheck1script" its back to work.
I'm not sure why you added the above code as well as what is the relation of it, Do you need above code?
That code is there to NOT show any results when the page loads so the person can (on Step 1) enter the correct PIN... and only one result would display (this would be Step 2 where the person selected their Experience from the dropdown menu) if the correct PIN was entered.
The [wpv-no-items-found] part of the "smartlinkcheck1" View acts as a way to check if the person knows the correct PIN number.
Does that help?
Is there a different / better way to accomplish this?
When I checked the code you added and added a print_r() to check what results the view ID 19 offers:
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 19 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { echo "<pre>"; print_r($query_results); exit; // is there a filter set? if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 99, 3 );
It does have one result as you can see:
- hidden link
You can remove the break point code I've added once you verify it.
echo "<pre>"; print_r($query_results); exit;
Even though it has result but your have set the if condition to check meta_query and tax_query and search (s) as if condition becomes true you are setting the results of view to none as you can see with the code you added above.
Can you adjust your code and correct it first?
HHmmm. I'm not a developer so I have no idea what all of that means 🙂
I removed the code:
echo "<pre>";
print_r($query_results);
exit;
To see if the page was working (by working it should show the screen where it asks for the PIN) but it is not displaying.
Do you know how to get that back?
You can get back the screen when you remove the whole code:
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 19 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { echo "<pre>"; print_r($query_results); exit; // is there a filter set? if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 99, 3 );
You should try to remove the whole code and screen will be back to work and then you should tell me whats required.
Hi Minesh,
Thanks for the update. Maybe it's best if I describe what was happening.
SCREEN A) When any SmartLink url (like this one hidden link) loads the "smartlinkcheck1" View does NOT find any results because the Query Filter is looking at the URL for a parameter named "checkpin" and there isn't one yet... so it displays the "no results found" message. This message (which typically says something like "No results found") has a little intro message and a box for the person to enter in a PIN.
NOTE: The user must be logged in
SCREEN B) Once the person types in the PIN (like "4321", they have this PIN on a physical card in their hand) the forms reloads the page and puts that PIN in the URL as a variable for "checkpin". The View now finds 1 result which displays the "Loop item in smartlinkcheck1" which now includes the CRED form [cred_form form='slconfig'].
It was at this point where I submitted my original ticket.
I could not get the [cred_generic_field type='select' field='wpcf-slexperience'] to display the Experiences that this user has added.
Currently SCREEN A is no longer functioning (it is now just a white screen).
I added "THIS IS SCREEN B" and "THIS IS SCREEN B" to the "smartlinkcheck1" View to demonstrate.
The Settings > Custom Code > smartlinkcheck1script was there to just not show any results UNLESS there is a filter (URL variable in this case) is provided. This was working.
Does that explanation help?
My ticket request was to simply get the dropdown [cred_generic_field type='select' field='wpcf-slexperience'] to display the Experiences that this user has added.
You were using the wrong hook so what I can do.
To display no results when there is no URL param set namely "checkpin", I used the "wpv_filter_query" hook and if there is no URL param set in the URL with name "checkpin" I just set to display post__in to 0. as you can see with the following code:
I've changed the code to "Custom Code" section as given under:
function func_set_view_result_empty_no_url_param( $query_args, $view_settings, $view_id ){ $target_views = array( 19 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views) and !isset($_GET['checkpin']) ) { $query_args['post__in'] = array(0); } return $query_args; } add_filter( 'wpv_filter_query', 'func_set_view_result_empty_no_url_param', 10, 3 );
Then, to display the view as source of the generic field as shared before, I used the following view:
function func_filter_by_shortcode_attribute( $query, $view_settings, $vid ){ global $current_user; $target_view_ids = array( 39 ); // Edit to add IDs of Views to add this to if ( in_array( $vid, $target_view_ids ) ) { $query['meta_query'][0]['value'] = $current_user->ID; } return $query; } add_filter( 'wpv_filter_query', 'func_filter_by_shortcode_attribute', 99, 3 );
I would also say there is hard caching is on with your server maybe due to WP Engine as when I make change it reflects after 10-20 mins or so.
I can see the dropdown is generated as expected: hidden link
Screenshot: hidden link
My issue is resolved now. Thank you!