Skip Navigation

[Resolved] Filter a View by Buddypress Member as post author

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to show a View of a custom post type filtered by post author, where the BuddyPress displayed member is the post author.

Solution: Use the Buddypress PHP API to create a custom shortcode that returns the Member's User ID. Pass the user ID into the post author filter as a shortcode attribute.

// custom shortcode to return displayed BP Member's user ID
add_shortcode( 'tssupp-get-bp-member-user-id', 'tssupp_get_bp_member_user_id_func');
function tssupp_get_bp_member_user_id_func($atts)
{
  if( function_exists('bp_displayed_user_id')){
    return bp_displayed_user_id();
  }
  return null;
}

Register tssupp-get-bp-member-user-id in Toolset > Settings > Front-end Content: Third party shortcode arguments

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/
https://codex.buddypress.org/developer/playing-with-the-users-id-in-different-contexts/

This support ticket is created 4 years 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 2 voices.

Last updated by renatoL-2 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1922215

Same problem. You can help? I also need this filter.

When I click on a member, the Site must show the events recorded by that user.

hidden link

This is the URL used by Buddypress. I need to create a filter where the authors of the event are the same as this member. How do I get that member?

#1922247
Screen Shot 2021-01-28 at 1.57.56 PM.png

Hi, I assume you are using a View to display Events on this page, is that correct? I can show you how to filter a View by post author, but I'm not sure how to access author information based on a Buddypress URL. I'm not trained in their software, so I'm not sure how to access that information. Their support team would be better able to provide information about that.

If you are able to get the User's username or ID from the Buddypress team using a shortcode, I can help you implement that information in a View of Events as a post author Query Filter, using a shortcode attribute to set the User dynamically. See the screenshot here showing how to configure a View's post author filter to accept a shortcode attribute. When a View of Events is configured this way, you can use a View shortcode to insert this View in your page template like so:

[wpv-view name="Your Events View" author="[buddypress-shortcode][/buddypress-shortcode]"]

If the Buddypress team cannot provide the Member ID or username using a shortcode, but can provide the information using a PHP API, I can show you how to use our Views PHP API to modify a View's post author query filter configurations using their PHP API. I would need to see an example of their PHP API that can provide the Member's username or ID to be able to help with that implementation.

Let me know if you need anything from me to help facilitate that conversation with their support team, or if I have misunderstood what you want to achieve.

#1922311

I searched around here and didn't get any shortcode. What I got was this code that I don't know how I can use it.
<? php echo bp_displayed_user_id (); ? >

#1922313

I found this article that seems to explain it well, but I don't know where or how to work with these codes

hidden link

#1922317

Okay great, let's see if that function gives us the necessary information. Please add this code to your child theme's functions.php file, or create a new custom code snippet in Toolset > Settings > Custom Code tab and add the code there:

// custom shortcode to return displayed BP Member's user ID
add_shortcode( 'tssupp-get-bp-member-user-id', 'tssupp_get_bp_member_user_id_func');
function tssupp_get_bp_member_user_id_func($atts)
{
  if( function_exists('bp_displayed_user_id')){
    return bp_displayed_user_id();
  }
  return null;
}

If you create a new snippet in Toolset > Settings > Custom Code, set the snippet to run everywhere and activate the snippet.

Then edit the template for the Member Events page and place the custom shortcode in that template like this:

BP Member User ID: [tssupp-get-bp-member-user-id][/tssupp-get-bp-member-user-id]

After that, test the Events page for various Members. If the shortcode returns the correct User ID for each Member, we have the information we need. I'll show you how to proceed with this information.

#1922337

Vou tentar seguir as dicas e posto o resultado

#1922339

Sorry, I don't understand your last comment. Would you like to continue discussing in English? If not, I can transfer this ticket to a Spanish speaking supporter.

#1922345
Capturar.JPG

It worked perfectly well. We managed to get the ID. Now I need to make the filter so that only the posts appear in which the author of the post = id we took.

I tried to put the shortcode in the filter but it didn't work. I believe we are close.

#1922355

Let's go to Google translator because you are a genius.

#1922357
Capturar.JPG

See if I'm on the right track?

I try to put the shortcode in the filter but the error message appears.

#1922385

I try to put the shortcode in the filter but the error message appears.
You should not add the shortcode here. Instead, you should replace the text "author" in the filter settings.

Next, go to Toolset > Settings > Front-end Content and add "tssupp-get-bp-member-user-id" in the "Third-party shortcode arguments" field. This will allow us to use the custom shortcode in a View shortcode.

Next, you must insert the View in your template using a shortcode and set the author attribute like this:

[wpv-view name="your-view-slug" author="[tssupp-get-bp-member-user-id][/tssupp-get-bp-member-user-id]"]

Replace your-view-slug with the slug of this View, or the name of the View.

#1922497

My issue is resolved now. Thank you!