I am trying to: Create a filter to query from a custom short-code.
I am trying to create a query in views that will query current user's custom field and match it with a custom field in a posttype. To do so, I've created a custom shortcode that looks at user table and grabs a custom field and returns as a string.
I am having hard time using the output of this shortcode as input of the query.
code for shortcut:
function shortcode_HelloWorld() {
$user = wp_get_current_user();
$variable = get_field('acf_county', 'user_'. $user->ID);
return $variable[0]->post_title;
}
add_shortcode('getcounty', 'shortcode_HelloWorld');
function prefix_add_my_shortcodes($shortcodes) {
$shortcodes[] = 'getcounty';
return $shortcodes;
}
add_filter('wpv_custom_inner_shortcodes', 'prefix_add_my_shortcodes');
Link to a page where the issue can be seen:
I expected to see:
Instead, I got:
Don't use View Param in the query, instead, do this:
- set the Views Query to "set by the View shortcode attribute "your_attribute" eg. [wpv-view name="view-name" your_attribute ="1"]"
- Then, insert the View as ShortCode to a page and pass the ShortCode attribute, populate it with your Custom ShortCode:
[wpv-view name="view-name" your_attribute ="[your-shortcode]"]
That will then plot the view, listening to the ShortCode attribute, which is populated by your Custom ShortCode.
Of course, the Custom ShortCode should return values that match existing values compared by the ShortCode attribute.
Does that work?
My issue is resolved now. Thank you Soo soo much!
Just a note that I was unable to nest shortcodes (shortcode in a shortcode) in my (elementor) theme, BUT I was able to declare my own shortcode and then call the wpv-view shortcode inside that one. So here's what I did:
in functions.php:
/* getting the view shortcode and passing in the user's db letter */
function get_user_view(){
return do_shortcode('[wpv-view name="listing-search" userdbletter="'.get_user_db_letter().'"]');
}
add_shortcode('getuserview','get_user_view');
function get_user_db_letter(){
// more functionality coming here..
return 'Y';
}
in theme page editor (using a text block):