How can I access (not-registered) query params in MB Views?
- This topic has 3 replies, 2 voices, and was last updated 4 years, 10 months ago by
Long Nguyen.
-
AuthorPosts
-
June 8, 2020 at 6:54 PM #20278
Dominik Wever
ParticipantHi guys,
I'm loving MB Views, and I've already created quite a complex view accessing different CPTs with loops, repeating field groups, etc. - all working like a charm! Doing this, I've also made use of the "mb.php_function" trick to call PHP functions directly within MB Views.
Now here's where I'm stuck:
I need to access a URL query param (called "zrole") in the view (one that is NOT registered with WordPress), but I can't find a way to access it:
I've tried things like
{% set zRole = mb._GET['zrole'] %}
zRole from Query Param: {{ zRole }}but this doesn't return anything. Changing to the following results in a complete fatal error:
{% set zRole = mb.$_GET['zrole'] %}
zRole from Query Param: {{ zRole }}I can't use mb.get_query_var(), because the query var is custom and NOT registered with WordPress.
Any trick how I can access the above URL query var inside my view?
Thanks a lot!
DominikJune 8, 2020 at 8:56 PM #20280Long Nguyen
ModeratorHi,
To get the URL query var, you can use the function
get_query_var()
supported by WordPress. The code in View should be{{ mb.get_query_var( 'zrole 'Default value' ) }}
. Please follow the documentation https://developer.wordpress.org/reference/functions/get_query_var/.FYI, you can also create your custom function, then use it via
mb.
proxy in View. Let's try with sample functionfunction get_my_query_var() { return $_GET['zrole']; }
then use in View
{{ mb.get_my_query_var() }}
.Let me know how it goes.
June 8, 2020 at 9:40 PM #20282Dominik Wever
ParticipantHi Long,
thanks a lot for your quick answer!
As mentioned in the documentation that you've linked (and that I had checked before writing you), get_query_var ONLY SUPPORT STANDARD WORDPRESS QUERY VARS, and NOT my custom query vars like "zrole":
get_query_var() only retrieves public query variables that are recognized by WP_Query. This means that if you create your own custom URLs with their own query variables, get_query_var() will not retrieve them without some further work (see below).
I want to AVOID writing a custom function and adding it to functions.php to keep all the code nicely together in the MB View.
So I tried mb.get_query_var, but it doesn't give me my custom query var because of the above-stated reasons.
Is there a way to access the PHP $_GET function via the MB. proxy or another way to access the custom query var WITHOUT adding something to functions.php?
Thanks again,
DominikJune 9, 2020 at 7:28 AM #20286Long Nguyen
ModeratorHi,
It is impossible to use a customization work without using a custom function. You can add your custom query var to the public query variables with the filter hook
query_vars
https://developer.wordpress.org/reference/functions/get_query_var/#custom-query-vars.And if you don't want to add the code to the file functions.php, you can use the plugin Code Snippets to implement the code.
-
AuthorPosts
- You must be logged in to reply to this topic.