Navigation überspringen

[Gelöst] Conditionally display a map based on a filtered view

This support ticket is created vor 3 Jahren, 5 Monaten. 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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

Dieses Thema enthält 7 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von NickC8880 vor 3 Jahren, 5 Monaten.

Assistiert von: Jamal.

Author
Artikel
#2116917

Tell us what you are trying to do?
As a follow-on from this ticket - https://toolset.com/forums/topic/filtering-views-query-by-author-for-a-profile-page/ - I am displaying a content template in a tab of a BuddyBoss user profile. The template contains a map and a view, and the view is filtered via the previously provided code to determine which profile is being viewed.

I would like to hide the map if the address field is empty, but I cannot figure out how to do this through the blocks UI, be it with a Conditional Block or otherwise. Are you able to provide a workflow or code that might help?

Thank you.

What is the link to your site?
versteckter Link

#2117311

Hello and thank you for contacting Toolset support.

You can use a conditional block and put the map inside of it. On the conditional block condition, check if the address field is different than an empty string(just leave the value field empty). Check this article https://toolset.com/course-lesson/using-toolset-conditional-block/

I hope this helps. Otherwise, I'll need to take a closer look at your website and I will need more details about the field and the profile page. Let me know, so I can activate a private reply.

#2117403

Thank you! However, I can't seem to make that work. If you can activate a private reply, I'll send the login info for you to take a look. But here is more info for now.

The user data, including the address, is stored in the User Profiles custom post type, which has a Content Template. The template is being displayed by inserting its shortcode in a tab created by another plugin. The content template's view is filtered by the custom code that Christian provided in the previously referenced ticket, so as to show only the User Profile CPT of whichever BuddyBoss user is currently displayed (as opposed to the currently logged-in user).

It correctly shows the User Profile fields, like First Name, but the map is outside of the View, within a conditional block. I can't seem to get any setting, including the one you suggested, to target the filtered post from within the view, which is in the same content template.

I'm sure it'll all be easy to understand when you take a look. I'll provide links to each aspect of what I've described.

#2117453

Sure. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Yes, please, provide links to each aspect you have described(content template, custom code, etc.)
I am sure there will be a way to make it work. The most important thing is that the content template needs to be bound to that specific profile post.

#2117523

Well, what you were getting is somehow expected. The content template does not know what post it should display.

There are two ways to pass the post to the content template:
- Put the content template inside the loop of a view that will return the user's UserProfile.
- Pass the ID of the user profile to the content template in the item attribute. https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

Because the content template includes the view that you hooked using Christian's code, we can't put it inside of the view's loop. So, I choose to use the second option(passing the ID to the content template using the item attribute).

For that, I created a new shortcode displayed-bp-user-id. This shortcode will return the UserProfile's ID of the visited bp user profile. This is the code.

add_shortcode('displayed-bp-user-id', 'my_displayed_bp_user_id');
function my_displayed_bp_user_id(){
  $author_id = function_exists('bp_displayed_user_id') ? bp_displayed_user_id() : 0;
  if ( $author_id == 0 ) return 0;
  
  $posts = get_posts( array(
    'post_type' => 'user-profile',
    'author' => $author_id,
    ) );
  if ( count( $posts ) == 0 ) return 0;
  
  return $posts[0]->ID;
}

I added this code on the same snippet that you are using with the view.

I then, registered the shortcode in Toolset->Settings->Front-end Content(tab)->Third-party shortcode arguments. So, it can be used inside of other Toolset shortcodes(wpv-post-body)

Then, I used it in the content template shortcode:

[wpv-post-body view_template="template-for-user-profiles" item="[displayed-bp-user-id]"]

I hope this explains that a content template needs to know what post to display. And that everything I explained before makes sense to you.

I just want to add, that because we have passed the post's ID to the content template, there is no need to use a view inside of it. You can just display the information outside of the view, and drop the view and its custom code.

Let me know if you have any further questions.

#2117537

Thanks so much for the quick work! This seems to be a very nice, simple and flexible solution.

To clarify, Christian's view filter is no longer needed because the shortcode will tell the content template which post it should be displaying?

A related question - is using a content template the best way to even be doing this? Could it all have been done by creating a page with a view, and filtering that view with Christian's code? Or a page with Single Fields etc... that get filtered with your shortcode?

I ask because it is possible that I will add other fields from other posts to that same User Profile tab, and it seems that this method can only work off of one post type at a time. Or can I add additional posts to the get_posts array? If that is possible, would you mind modifying the code to show me how I would add multiple post types? For instance, Christian's code has this line, which allows me to add any view IDs.

  $views = array( 123, 456 ); 

Could I use something like this?

  'post_type' => array('user-profile', 'posttype2', 'posttype3'), 

Anyway, thanks again. I will experiment with all of this today to get a better understanding of it and follow up if I have any further questions/issues with it.

#2117539

To clarify, Christian's view filter is no longer needed because the shortcode will tell the content template which post it should be displaying?
Exactly. Keep in mind that a content template displays ONLY ONE post. Where as a view is meant to query the database for a list of posts and display them. You can use a content template inside the loop of the view, and it will display each post from the view's results.

A related question - is using a content template the best way to even be doing this? Could it all have been done by creating a page with a view, and filtering that view with Christian's code? Or a page with Single Fields etc... that get filtered with your shortcode?
Well, you can decide what to use/choose. Just keep in mind that a content template is meant to display one post, whereas a view is meant to display a list of posts(that can be paginated, and filterable with search form, etc...). So, you decide 🙂

Or can I add additional posts to the get_posts array? If that is possible, would you mind modifying the code to show me how I would add multiple post types?
No. Because in that case we won't be sure that the shortcode will return the ID of the UserProfile post, or another post type.
We can adapt the code to query multiple posts types, but that's not useful. The shortcode is meant to return the visited user's UserProfile post, nothing else.
Depending on what you would like to do, there is most probably a better solution.

Feel free to ask any further questions if you still need more details or you have any doubts about anything.

However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I have to kindly ask you to open a new thread if you need assistance with a different matter.

#2117541

Thanks very much for the additional information! I definitely don't yet have a complete conceptual grasp of all of this yet, but this has been very helpful in improving my understanding. I very much appreciate the patience and assistance you all have given me so far.

I'll close this ticket now and experiment with it all for a while. If I have any other specific questions, I will open a new ticket.