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/