Created
May 14, 2018 06:10
-
-
Save raviousprime/b35211c906904d1d3e3dfe4bc9a0318a to your computer and use it in GitHub Desktop.
Buddyblog subnav to show following user posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add new tab in buddyblog nav items | |
*/ | |
function buddydev_add_buddyblog_subnav_item() { | |
if ( ! function_exists( 'buddyblog' ) || ! function_exists( 'bp_follow_get_following' ) ) { | |
return; | |
} | |
bp_core_new_subnav_item( array( | |
'name' => __( 'Following User', 'buddydev' ), | |
'slug' => 'following_user_posts', | |
'parent_slug' => BP_BUDDYBLOG_SLUG, | |
'parent_url' => buddyblog_get_home_url(), | |
'screen_function' => 'buddydev_following_users_posts', | |
'show_in_admin_bar' => true, | |
) ); | |
} | |
add_action( 'bp_buddyblog_setup_nav', 'buddydev_add_buddyblog_subnav_item' ); | |
/** | |
* Callback screen function | |
*/ | |
function buddydev_following_users_posts() { | |
add_action( 'bp_template_content', 'buddyblog_render_following_user_posts' ); | |
bp_core_load_template( 'members/single/plugin' ); | |
} | |
/** | |
* Render following user posts | |
*/ | |
function buddyblog_render_following_user_posts(){ | |
add_filter( 'paginate_links', 'buddydev_modify_buddyblog_pagination_link' ); | |
add_action( 'pre_get_posts', 'buddydev_modify_posts_query' ); | |
buddyblog_load_template( 'posts.php' ); | |
} | |
/** | |
* Filter main query for subnav context | |
* | |
* @param WP_Query $query Query object. | |
*/ | |
function buddydev_modify_posts_query( $query ) { | |
if ( bp_is_current_component( 'buddyblog' ) && bp_is_current_action( 'following_user_posts' ) ) { | |
$follwing_user_ids = bp_follow_get_following( array( 'user_id' => bp_displayed_user_id() ) ); | |
$paged = bp_action_variable( 1 ); | |
$paged = $paged ? $paged : 1; | |
$query->set( 'post_type', buddyblog_get_posttype() ); | |
$query->set( 'author', '' ); | |
$query->set( 'author__in', $follwing_user_ids ); | |
$query->set( 'post_status', 'publish' ); | |
$query->set( 'paged', intval( $paged ) ); | |
} | |
} | |
/** | |
* Modify buddyblog pagination link | |
* | |
* @param string $link Link string. | |
* | |
* @return string | |
*/ | |
function buddydev_modify_buddyblog_pagination_link( $link ) { | |
if ( strpos( $link, BUDDYBLOG_ARCHIVE_SLUG ) !== FALSE ) { | |
$link = str_replace( BUDDYBLOG_ARCHIVE_SLUG, 'following_user_posts', $link ); | |
} | |
return $link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment