Home › Toolset Professional Support › [Resolved] How to include third-party variables within a Views shortcode
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)
Tagged: Views, Views plugin
This topic contains 15 replies, has 2 voices.
Last updated by ericE-4 4 years, 3 months ago.
Assisted by: Minesh.
I am trying to display a view that includes a filter for two fields: Location and Province. the values of both of those filter variables will come from a third-party plugin, through shortcodes. For example, if I simply place this variable (I don't know if it's officially called a shortcode or what) on the page:
{{mpg_location}}
it will replace that variable with a value like "Outer Rim". Similarly, {{mpg_prov}} will be replaced with something like "BC" when the page is rendered.
I need to use the output of these variables inside a Views shortcode, to properly filter the query by those fields. I have used:
[wpv-view name="display-all-lodges-in-same-location-map" prov="{{mpg_prov}}" location="{{mpg_location}}"]
However when the page is rendered, the view shows no results, presumably because the Views shortcode is rendered BEFORE the variables are replaced, so it's actually trying to display a view with the LITERAL filters of "{{mpg_location}}" and "{{mpg_prov}}".
How can I ensure that the variables are replaced BEFORE the Views shortcode is executed?
On this dynamically-built page: hidden link
under the heading "OTHER FISHING LODGES IN ALASKAN INTERIOR" you can see the results of a text field showing the variables being properly replaced, and below that the result of the shortcode result.
Hello. Thank you for contacting the Toolset support.
Basically - when you want to use the third-party plugin shortcode as a view's shortcode argument, you will require to register such shortcode at:
=> Toolset => Settings => Front-end Content => Third-party shortcode arguments
Once you register that, you will require to make sure that to content you are passing as shortcode argument actually exist with your database.
Can you please register the shortcode you are using and check if that help you to resolve your issue.
If above information does not help, I will require problem URL and access details for further investigation.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) 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.
Hi Eric,
Thank you for sharing access details. I see you are using the following plugin:
=> hidden link
The strange thing is that the shortcode you are using to get the prov value: {{mpg_prov}} is not get parsed when using views.
I tried creating the custom shortcode and all other ways but shortcode {{mpg_prov}} value does not get parsed.
Can you please check with your plugin author about how we can get the value that is returned by the shortcode {{mpg_prov}} using PHP way?
Strange thing is that when I echo the shortcode value and add exit; statement I get the value but somehow when I try to use that with the view.
If you can check with plugin author and check with them how we get the {mpg_prov}} shortcode value using PHP rather using shortcode that will be helpful further.
I asked the plugin developer about this and this is what they said:
"If you have good knowledge of PHP you can "disassemble" MPG and try to play with MPG_CoreModel::mpg_shortcode_replacer($str, $project_id) that replace {{shortcodes}} to related value from the source file.
You may try to pass as string something like {{mpg_shortcode}} and set the returned value as a global variable, that will be used as value for any needs.
But if Toolset expect some value in shortcode (like a [toolset attr="mpg_returned_value"]) you need to slip this global variable in code of plugin (you can't get value of global variable from MPG in text editor for using in another plugins)."
Unfortunately I don't have very good knowledge of PHP. So here's another idea: is it possible to execute the Toolset [wp-view] shortcode on demand with javascript or AJAX? That way I could have the page render which replaces the {{mpg_prov}} variable with the proper data, and then after 1 second I can1 then run the Toolset shortcode, using the variables which are now on the page as inputs. Or even better, I can execute the Toolset shortcode only when the user has scrolled to the section where it's located, like a lazy load.
Thank you for the details.
I've removed the Query Filter you added from the "Query Filter" section from the following view:
=> hidden link
I've added the following code to "Custom Codde" section offered by Toolset to filter the view using the view's filter query "wpv_filter_query":
=> hidden link
function func_filter_by_mpg_shortcode_value( $query_args ,$view_settings, $view_id ) { global $post; global $WP_Views; global $mpg_prov; if ( $view_id == 21836 ) { $mpg_prov = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_prov}}', 1); if(empty($query_args['meta_query'])){ $query_args['meta_query'][] = array('key'=>'wpcf-prov-state', 'value'=> $mpg_prov, 'type'=> 'CHAR', 'compare'=> "=" ); } } return $query_args; } add_filter( 'wpv_filter_query', 'func_filter_by_mpg_shortcode_value', 10, 3);
I can see the view is successfully filter the value using the dynamic value that is coming from the following link of the code:
$mpg_prov = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_prov}}', 1);
Can you please confirm it works as expected.
That works great! One last thing...could you help me modify the code so it also includes the shortcode {{mpg_location}} which supplies the value for the field wpcf-region and uses these two filters with an AND operator? Like this:
Prov/state (wpcf-prov-state) = {{mpg_prov}} AND Region (wpcf-region) = {{mpg_location}}
I could not able to login to your site.
Can yo please try to use the following code and try to resolve your issue:
function func_filter_by_mpg_shortcode_value( $query_args ,$view_settings, $view_id ) { global $post; global $WP_Views; global $mpg_prov; global $mpg_region; if ( $view_id == 21836 ) { $mpg_prov = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_prov}}', 1); $mpg_region = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_location}}', 1); if(empty($query_args['meta_query'])){ $query_args['meta_query'][] = array('key'=>'wpcf-prov-state', 'value'=> $mpg_prov, 'type'=> 'CHAR', 'compare'=> "=" ); $query_args['meta_query'][] = array('key'=>'wpcf-region', 'value'=> $mpg_region, 'type'=> 'CHAR', 'compare'=> "=" ); } } return $query_args; } add_filter( 'wpv_filter_query', 'func_filter_by_mpg_shortcode_value', 10, 3);
It was a tricky solution but happy to share final solution and see it working 🙂
Can you please confirm it works as expected and solution I shared help you to resolve your issue.
It ALMOST works great! The only problem I still have is that the query seems to perform differently from how it does if I hard-code the query values. For instance, when I set the filter for Prov/State = Yukon" AND Region = null, it returns all records where the Province is Yukon.
However, with the code-based query, no records are returned. What can I do to make it return the Yukon records and ignore the empty Region?
The code based query is dependent on the values get from the third-party plugin shortcode {{mpg_prov}} and {{mpg_location}}.
So, you can not set any static filters now as values passed to query is from the third-party plugin shortcode.
Do you still want to pass the static values to your view by setting up the Query filter for your view? If yes, I would like to know when to pass the shortcode values to query and when to use the query filter you setup from the view (the hard coded values).
I think one of us is getting confused. Maybe I should first ask how should I set up a query filter such that:
- filter values come from shortcode attributes "prov" and "location"
- the "location" shortcode attribute is not specified
- the records returned include all those that have a Province/State = Yukon.
Whatever way I would set that up—>I need the custom filter code to do the same thing.
Note that I DO NOT want to pass any static values by setting up a query filter for my view. The PHP code-based filter that you wrote for me is exactly what I need, I just need to get it to return the records in the same way.
So, accessing what URL the shortcode {{mpg_prov}} will get me the value Yukon and {{mpg_location}} shortcode should return the null?
This URL will generate values "Yukon" and null: hidden link
Thank you for sharing the URL but as said with my previous message I could not able to login to admin.
Can you please setup admin access details for me or share/activate auto-login url?
I have set the next reply to private which means only you and I have access to it.
I'm not sure why you move the code to PHP Inserter as Toolset already offers the place "Custom Code" where you can keep your Toolset related custom code. As it allow us to modify and troubleshoot the code quick way. Like if there is error in the code the whole side will not be down. I'm not sure how PHP Inserter works.
However - its your choice where you want to put such code.
I've adjusted the code as given under and commented the code in section PHP Inserter and added to the "Custom Code" section of Toolset. Can you please confirm it works as expected now.
function func_filter_by_mpg_shortcode_value( $query_args ,$view_settings, $view_id ) { global $post; global $WP_Views; global $mpg_prov; global $mpg_region; if ( $view_id == 21836 ) { $mpg_prov = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_prov}}', 1); $mpg_region = MPG_CoreModel::mpg_shortcode_replacer('{{mpg_location}}', 1); if(empty($query_args['meta_query'])){ if(!empty($mpg_prov)){ $query_args['meta_query'][] = array('key'=>'wpcf-prov-state', 'value'=> $mpg_prov, 'type'=> 'CHAR', 'compare'=> "=" ); } if(!empty($mpg_region)){ $query_args['meta_query'][] = array('key'=>'wpcf-region', 'value'=> $mpg_region, 'type'=> 'CHAR', 'compare'=> "=" ); } } } return $query_args; } add_filter( 'wpv_filter_query', 'func_filter_by_mpg_shortcode_value', 10, 3);