bbp_pre_get_posts_normalize_forum_visibility( WP_Query $posts_query = null )
Adjusts forum, topic, and reply queries to exclude items that might be contained inside hidden or private forums that the user does not have the capability to view.
Description
Doing it with an action allows us to trap all WP_Query’s rather than needing to hardcode this logic into each query. It also protects forum content for plugins that might be doing their own queries.
Parameters
- $posts_query
-
(Optional)
Default value: null
Return
(WP_Query)
Source
File: bp-forums/forums/functions.php
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 | function bbp_pre_get_posts_normalize_forum_visibility( $posts_query = null ) { // Bail if all forums are explicitly allowed if ( true === apply_filters( 'bbp_include_all_forums' , false, $posts_query ) ) { return ; } // Bail if $posts_query is not an object or of incorrect class if ( ! is_object ( $posts_query ) || ! is_a ( $posts_query , 'WP_Query' ) ) { return ; } // Get query post types array . $post_types = ( array ) $posts_query ->get( 'post_type' ); // Forums if ( bbp_get_forum_post_type() === implode( '' , $post_types ) ) { // Prevent accidental wp-admin post_row override if ( is_admin() && isset( $_REQUEST [ 'post_status' ] ) ) { return ; } /** Default ***********************************************************/ // Get any existing post status $post_stati = $posts_query ->get( 'post_status' ); // Default to public status if ( empty ( $post_stati ) ) { $post_stati = array ( bbp_get_public_status_id() ); // Split the status string } elseif ( is_string ( $post_stati ) ) { $post_stati = explode ( ',' , $post_stati ); } /** Private ***********************************************************/ // Remove bbp_get_private_status_id() if user is not capable if ( ! current_user_can( 'read_private_forums' ) ) { $key = array_search ( bbp_get_private_status_id(), $post_stati ); if ( ! empty ( $key ) ) { unset( $post_stati [ $key ] ); } // ...or add it if they are } else { $post_stati [] = bbp_get_private_status_id(); } /** Hidden ************************************************************/ // Remove bbp_get_hidden_status_id() if user is not capable if ( ! current_user_can( 'read_hidden_forums' ) ) { $key = array_search ( bbp_get_hidden_status_id(), $post_stati ); if ( ! empty ( $key ) ) { unset( $post_stati [ $key ] ); } // ...or add it if they are } else { $post_stati [] = bbp_get_hidden_status_id(); } // Add the statuses $posts_query ->set( 'post_status' , array_unique ( array_filter ( $post_stati ) ) ); } // Topics Or Replies if ( array_intersect ( array ( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), $post_types ) ) { // Get forums to exclude $forum_ids = bbp_exclude_forum_ids( 'meta_query' ); // Bail if no forums to exclude if ( ! array_filter ( $forum_ids ) ) { return ; } // Get any existing meta queries $meta_query = ( array ) $posts_query ->get( 'meta_query' , array () ); // Add our meta query to existing $meta_query [] = $forum_ids ; // Set the meta_query var $posts_query ->set( 'meta_query' , $meta_query ); } } |
Changelog
Version | Description |
---|---|
bbPress (r3291) | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.