How can I access (not-registered) query params in MB Views?

Support MB Views How can I access (not-registered) query params in MB Views?Resolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #20278
    Dominik WeverDominik Wever
    Participant

    Hi 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!
    Dominik

    #20280
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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 function

    function get_my_query_var() {
        return $_GET['zrole'];
    }

    then use in View {{ mb.get_my_query_var() }}.

    Let me know how it goes.

    #20282
    Dominik WeverDominik Wever
    Participant

    Hi 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,
    Dominik

    #20286
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.