Site icon Hip-Hop Website Design and Development

How to add pagination to a post loop in a custom BuddyPress tab

I created a new tab called “Videos” inside of the BuddyPress Profile page. This tab contains all video posts added by that user. The problem is that it only shows the first 12 posts and the pagination does not show up. I did try to include the pagination code provided by the theme but to no avail.

Notes:

Thank you in advance for all your help 😀

Code:

    <?php function my_videos_screen_content() { 
?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$args = array(
    'post_type'         => array( 'video' ),
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$author_videos = new WP_Query( $args );

if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php do_action( 'mars_pagination', null );?>
    <?php wp_reset_postdata();
    endif; ?>
        </div>
    </div>
</div>
</div>  
    <?php }

So I figured out how to show the pagination, with the following code.
The problem is when I click on the next page, it brings me to a 404 page. But when I manually visit a custom URL it does show the second page correctly, here are both examples:

404 Version:

This is the url structure that is generated by this pagination code

www.website.com/members/username/my-videos/page/2/

Working Version:

This is a manual url I found to be working

www.website.com/members/username/my-videos/?page=2

Question is, how to get it to work with the first version (/page/2/)

CODE:

    <?php function my_videos_screen_content() { 
echo 'We are currently working on this section, some content may not appear properly. Thank you for your patience.'; ?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$custom_query_args = array(
    'post_type'         => 'video',
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$custom_query = new WP_Query( $custom_query_args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php endif; 
    wp_reset_postdata(); ?>

    <?php // Custom query loop pagination
    previous_posts_link( 'Newer Videos' );
    next_posts_link( 'Older Videos', $custom_query->max_num_pages );?>

    <?php // Reset main query object
    $wp_query = NULL;
    $wp_query = $temp_query; ?>

        </div>
    </div><!-- /.row -->
</div>
</div><!-- /.container -->