Skip Navigation

[Resolved] Passing filter values to a view with a shortcode

This support ticket is created 6 years, 1 month 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.

This topic contains 2 replies, has 2 voices.

Last updated by helenmaryC 6 years, 1 month ago.

Author
Posts
#1113231

I have a CPT called "Dogs." I need to list dogs filtered by a list of author ids, so I designed a view with a shortcode filter for author id. if I insert the following in a page it works: [wpv-view name="dogs-for-rosettes" author="135, 136"]

The list of author ids needs to be generated based on the current user. Each user has a custom field called primary-id which links them to other users in their family, one of which will have the same ID and primary-id. I created a shortcode to generate a list of comma-delimited user ids as follows:

//Create array of linked users
function crfcrc_linked_users(){
    $linkedUsers = [];
    $current_user = wp_get_current_user();
    $userID = $current_user->ID;
    $primaryID = get_user_meta($userID, 'wpcf-primary-id', true);
    $user_query = new WP_User_Query( array( 'meta_key' => 'wpcf-primary-id', 'meta_value' => $primaryID ) );
    // User Loop
    if ( ! empty( $user_query->get_results() ) ) {
        foreach ( $user_query->get_results() as $user ) {
            $linkedUsers[] = $user->ID;
        }
    }
    return $linkedUsers;
}
//END Create array of linked users

//Shortcode: Comma-delimitted list of linked users
function crfcrc_linked_users_cd() {
    $linkedUserArray = crfcrc_linked_users();
    $linkedUserCD = implode(", ", $linkedUserArray);
    return $linkedUserCD;
}
add_shortcode('crfcrc-linked-users-cd', 'crfcrc_linked_users_cd');
//END Shortcode: Comma-delimitted list of linked users

If I just put my shortcode on the page it shows exactly what I expect: 135, 136

if I then try to add the view to a page using [wpv-view name="dogs-for-rosettes" author=[crfcrc-linked-users-cd]] what shows on the page is a single ] and nothing else. If I look in developer console, there is a comment "View not found." I have tried adding double-quotes around my shortcode, single-quotes around the shortcode, and adding a closing tag, [/crfcrc-linked-users-cd], and the only change is that I get "], '], or [/crfcrc-linked-users-cd]] instead of ]. I have also tried modifying the code above to remove the space in the Implode function, but that didn't seem to help.

Is what I am trying to do possible? I have used Types shortcodes in this fashion when adding a view to a view and it worked, so it seems like it should be. I have a feeling that I am missing some php escape sequence or something, I know I can create the list by coding it in PHP, but I am just starting to get the hang of Views and would like to get this to work. Can you point me in the right direction?

Thanks!

#1113541

[wpv-view name="dogs-for-rosettes" author=[crfcrc-linked-users-cd]] would be wrong, it should be:

[wpv-view name="dogs-for-rosettes" author="[crfcrc-linked-users-cd]"]

Additionally, you should make sure to register your Custom ShortCode in Toolset > Settings > FrontEnd Content > Third-party shortcode arguments.
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

That should solve the issue

#1113596

I had a feeling it was something easy. I must have been searching on the wrong keywords because I hadn't seen that part of the documentation before.

My issue is resolved now. Thank you! Have a great rest of the weekend!