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.
Problem: I have a many-to-many relationship between two custom post types. In a two-column table View, I want to display the title of each post from post type A in the left column. In the right column, I want to display the average value of a custom field in all the related Type B posts.
Solution: There's nothing exactly like this built into the system, so it's probably best to create a custom shortcode that can be used to query related posts and calculate the average custom field value. See examples below.
Problem:
The problem this user is having is that their view isn't returning all the posts in the list. Solution:
In this user's case when the view was checked, they were ordering the view by a custom field.
Unfortunately not all the posts has this custom field filled in which caused them not to appear on the frontend.
It is not possible to sort items that have a null value .