Skip Navigation

[Resolved] Filtering Views Query by Author for a profile page

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

Problem: I have a View that I would like to filter by post author. That View may be displayed in a BuddyPress profile. I have a PHP function provided by BB that will help determine the User ID based on the profile being displayed.

Solution: Use the Views Filter API wpv_filter_query to programmatically set a post author filter in this View using the function provided by BuddyPress / BuddyBoss.

// Override the post author filter of a View shown in the BP User page to display only that User's posts.
// https://toolset.com/forums/topic/filtering-views-query-by-author-for-a-profile-page/
add_filter( 'wpv_filter_query', 'tssupp_bp_force_author_filter',99,3 );
function tssupp_bp_force_author_filter( $query_args,$views_settings, $view_id) {
  $views = array( 123, 456 );
  if ( in_array( $view_id, $views ) ){
    // post author equals bp_displayed_user_id() if function exists
    $query_args['author'] = function_exists('bp_displayed_user_id') ? bp_displayed_user_id() : 0;
  }
  return $query_args;
}

You would replace 123, 456 with a comma-separated list of View IDs which you would like to filter by post author. This post author filter will only work if the function provided by BP/BB returns the correct User ID. If you try to place this View on a page where the BP/BB function does not return the correct value, the View will not be filtered appropriately.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 5 months 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 9 replies, has 3 voices.

Last updated by NickC8880 3 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#2113541

*Tell us what you are trying to do?*
I have a site built on the BuddyBoss (buddypress) platform. Rather than use the built-in extended profile functionality, which seems to not really be accessible/usable outside of the profile page, I would like to use Custom Post Types and Fields to create an extended user profile.

I have created a Tab within the profile page that displays the User Profile template, but I am having trouble getting it to display each user's extended profile when viewing their page.

The second filter option - Post author is the author of the page where this View is shown - seemed like it would be perfect, however it always just display's the admin's profile details. I assume this is because BuddyBoss considers all pages to be created by the admin.

So, I'm trying to make use of the Author's username is set by the URL parameter. In my site, each user profile has the URL format domain.com/username.

But I can't figure out how to set the parameter to select the appropriate username. The examples in the document that I'm following seem to be for selecting specific users, rather than just extracting the slug. I also don't know how to use the "xxxxx", "?xxxx=", etc...

*Is there any documentation that you are following?*
https://toolset.com/documentation/user-guides/views/filtering-views-query-by-author/

*What is the link to your site?*
seeingtheforest.net

#2113653

Just to add a bit more context, this functionality will be crucial for me since I will use it not just for embedding User Profiles CPT into the BuddyBoss profile page tabs, but many other things as well - classifieds listings etc... I presume that the same solution can be used for all of this - I just need to be able to identify the owner/author of each built-in profile page, and using the URL seems to be the best way of doing this.

Of course, I'm open to any other suggestions as well.

Thanks!

#2113691

One more thought: perhaps it is possible to use the fifth option "Post author is" with some sort of shortcode that extracts the current page's slug (which is, in fact, the page "author" username)?

I'm not yet skilled enough to figure out how to do that, but maybe you folks do?

Again, any other suggestions are welcome. I'm just trying my best to learn how to solve this (and future issues) on my own.

#2113777

In lieu of using the URL, I discovered that I can use this function to find the displayed user id

bp_displayed_user_id();

I have confirmed that it works, and can then use that ID to extract the username.

Is it possible to use these somehow with the filter? Perhaps the 5th or 7th options?

#2114147

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will have to review the structure first how you configured your profile page and how exactly you are displaying the extended profile of the user.

Can you please share problem URL and admin access details and share one example profile and tell me what exactly you would like to display at what section or what part of the profile?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2114759

For some reason I'm not able to see anything in this Content Template:
hidden link

I see "Error loading block: Sorry, you are not allowed to read blocks of this post." - Maybe a permissions thing with this User profile?

By using this, I can pass a shortcode attribute to the view by using "username='$username'". However, it seems that I can only pass an attribute to wpv-view rather than wpv-post-body for a content template.
Yes that is correct, you can pass a post ID into a Content Template using the "item" shortcode attribute, and the Content Template will then display dynamic information in the context of that post, but it is not possible to pass arbitrary shortcode attributes into a wpv-post-body shortcode. Views created in the Block editor also have a nasty limitation that they cannot accept shortcode attributes. You would have to create the View elsewhere, like in the legacy editor, then insert it in a Block Editor template using a shortcode if you want to pass shortcode arguments to it. A PHP filtering system will be more straightforward for achieving what you want. See below.

As can be seen in the links above, the profile page URL is domain.com/username. If username could just be extracted from the URL and applied to the view filter, this should work
Unfortunately no, a View's query filter can respond to either a true URL parameter or a shortcode attribute, but not a URL component/slug like this. A PHP filter will be better suited for this filtering. See below.

I have added some code to functions.php that automatically sets the User Profile post title and custom field "username" to be the current user's username upon saving the Post Form "Form for User Profiles". This appears to work.
Not sure I understand, but I think as long as the post is created with this User set as the post author, you should be able to filter the View displayed in the BP profile area by post author ID successfully with a less convoluted solution. It only requires one filter snippet, no other workarounds. Since you have a nice function to get the displayed User ID, might as well use it directly with a PHP filter. See below.

I would also prefer to not have to use all of these workarounds with snippets, shortcodes, etc... Is there a way to make that work - simply have the content template's view properly identify the displayed user (via URL or bp_displayed_user_id();)
Neither of those are options for setting a View's Query Filter dynamically, as you've noticed you may use a true URL parameter or a shortcode attribute if you want to set dynamic filter criteria, or you can use the PHP API. Assuming this View will always be filtered by post author ID using the bp_displayed_user_id as the filter criteria, you should use the Views Filter API wpv_filter_query to modify the post_author query argument for the View directly. It will eliminate the need for other shortcode arguments and query filter workarounds with shortcodes. You have a PHP function from BP, might as well use PHP to filter the View. This snippet sets the post author filter information directly in the query using PHP. You should remove the Post Author Query Filter settings from the View, since this code overrides them anyway:

// Override the post author filter of a View shown in the BP User page to display only that User's posts.
// https://toolset.com/forums/topic/filtering-views-query-by-author-for-a-profile-page/
add_filter( 'wpv_filter_query', 'tssupp_bp_force_author_filter',99,3 );
function tssupp_bp_force_author_filter( $query_args,$views_settings, $view_id) {
  $views = array( 123, 456 );
  if ( in_array( $view_id, $views ) ){
    // post author equals bp_displayed_user_id() if function exists
    $query_args['author'] = function_exists('bp_displayed_user_id') ? bp_displayed_user_id() : 0;
  }
  return $query_args;
}

You replace 123, 456 with a comma-separated list of View IDs which you would like to filter by post author ID == bp_displayed_user_id(). If you have multiple Views displayed in the BP User page tabs, you may want to filter them, too. Add their numeric IDs here in a comma-separated list, to filter them all by post author ID equal to the value returned by that bp_displayed_user_id() function.

The Views Filter API wpv_filter_query is documented here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

One other note is that I saw a forum reply (which I can no longer find) by Christian Cox that said that using some sort of render function is preferred to using wpv-view.
Check the Programmer Reference for more info about the render_view function to render a View with PHP, the render_view_template function to render a Content Template with PHP, and other Views API Functions:
https://toolset.com/documentation/programmer-reference/
https://toolset.com/documentation/programmer-reference/views-api/

#2114775

Thanks so much for the detailed reply.

I have fixed the issue with the Error loading block. It was a Toolset Map block, not sure what happened exactly.

If that was a hindrance, you can now look again at hidden link to see how it is not filtering when I use content template.

I will review and digest the rest of this this evening and follow up when I have a handle on what you've described. Hopefully I can figure it out myself, otherwise hopefully you'll be able to help me sort this out tomorrow.

Thanks again!

#2114823

Ok, I've had time to process all of this. I think I more or less understand all the backstory you've provided about what views, blocks, etc... can do. I suspect most of it is beyond my capability to implement at the moment, but it is good to have a conceptual understanding of it all in the event that I need to ask for help in the future.

One question: are the limitations for shortcodes views in blocks a limitation inherent with blocks, or simply a limitation of how Toolset Blocks are currently set up? If the latter, are there plans to expand this functionality (and other functions more generally) to better match the power of the legacy toolset?

Anyway, after a bunch of tinkering, I figured out what was going on with the code you provided and it appears to work perfectly!

But just to confirm, I put that code in functions.php (is that the appropriate place for it? or can it go in Toolset custom code?) and added the post id for the relevant view. Now, whenever that view is displayed anywhere on the site, it will be filtered by the displayed author id? And the same will happen for any other view ids that I add to the array?

What I then did was put the shortcode for the User Profile Content Template (which contains both the view and associated map) into the Profile Tab creator field, which allows for the map to be displayed and properly filtered. It works as expected, without all my convoluted workarounds.

If this is how it should all work, then thank you very much - this is a very elegant way to solve this problem site-wide and will work perfectly for the majority of my future needs. I look forward to building out the rest of my site - hopefully mostly on a no-code basis!

#2115297

One question: are the limitations for shortcodes views in blocks a limitation inherent with blocks, or simply a limitation of how Toolset Blocks are currently set up? If the latter, are there plans to expand this functionality (and other functions more generally) to better match the power of the legacy toolset?
It's the latter, and I wouldn't expect it to change in the near future. Since the legacy workflow exists and there are workarounds available now, this is does not seem to be a high priority for our developers. It has been requested for a while without being implemented.

But just to confirm, I put that code in functions.php (is that the appropriate place for it? or can it go in Toolset custom code?)
Either is fine, you're welcome to put snippets in the theme, or in the snippets editor, or split them up between both areas. If you plan to use the theme and snippets on multiple sites, it might make more sense to put code snippets in the theme so they are easily shared. If you don't use a child theme, or if you don't plan to reuse these snippets elsewhere, it might make more sense to use Toolset's snippets area.

Now, whenever that view is displayed anywhere on the site, it will be filtered by the displayed author id? And the same will happen for any other view ids that I add to the array?
Basically yes, for any View IDs in the array, the results of those Views will always be filtered by post author equal to (whatever value is returned by the displayed user id function). The API code here will attempt to apply post author filtering regardless of where the View is placed on the site, so you should be careful when displaying these Views in areas of the site where the bp_displayed_user_id function does not return the desired author's User ID. I'm not sure how that function works, but I suspect it only returns the expected User ID value when executed in the context of the BP profile page. In those cases the View would be filtered by some invalid author ID, which could produce "no results found" unexpectedly.

#2115367

Thanks again for your help solving this issue, as well as for the very detailed replies - the additional information gives me a better understanding of how Toolset and WP work.

Edit: Sorry, I clicked 5 instead of 1 for the rating of how easy it was to solve this. It was, in fact, very easy, not very hard.