How do I solve bbpress plugin pagination problem using paginate_link()

As I am having difficulty creating the pagination. I explored this method of adding it directly includesforumstemplate.php in the plugin.

function bbp_has_forums( $args = array() ) {

    // Forum archive only shows root
    if ( bbp_is_forum_archive() ) {
        $default_post_parent = 0;

    // User subscriptions shows any
    } elseif ( bbp_is_subscriptions() ) {
        $default_post_parent = 'any';

    // Could be anything, so look for possible parent ID
    } else {
        $default_post_parent = bbp_get_forum_id();
    }

    $default_forum_search = bbp_sanitize_search_request( 'fs' );

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

    // Default argument array
    $default = array(
        'post_type'           => bbp_get_forum_post_type(),
        'post_parent'         => $default_post_parent,
        'post_status'         => bbp_get_public_status_id(),
        'posts_per_page'      => get_option( '_bbp_forums_per_page', 50 ),
        'orderby'             => 'menu_order title',
        'order'               => 'ASC',
        'no_found_rows'       => true,
        'ignore_sticky_posts' => true,
        'posts_per_page' => 7,
        'paged' => $paged, // page number is defined here. 
        // Conditionally prime the cache for last active posts
        'update_post_family_cache' => true
    );

    // Only add 's' arg if searching for forums
    // See https://bbpress.trac.wordpress.org/ticket/2607
    if ( ! empty( $default_forum_search ) ) {
        $default['s'] = $default_forum_search;
    }

    // Parse arguments with default forum query for most circumstances
    $r = bbp_parse_args( $args, $default, 'has_forums' );

    // Run the query
    $bbp              = bbpress();
    $bbp->forum_query = new WP_Query( $r );

    // Maybe prime last active posts
    if ( ! empty( $r['update_post_family_cache'] ) ) {
        bbp_update_post_family_caches( $bbp->forum_query->posts );
    }
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' =>  $bbp->forum_query->max_num_pages
    ));
    // Filter & return
    
    return apply_filters( 'bbp_has_forums', $bbp->forum_query->have_posts(), $bbp->forum_query );
}

This is not in function.php of the child theme, as this is the only place I can do this to be able to use paginate_links() anywhere I want it displayed. Or Is there anything I am missing?

In archive-forum, I did something like this.

<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8" id="forum_index">
                    <?php
                    set_query_var('table_title', __translate( 'Discussion Groups', 'bbpress' ));
                    bbp_get_template_part( 'content', 'archive-forum' );
                    ?>
                    <div class="forum_pagination font-weight-bold">
                        <?php echo paginate_links(); ?>
                    </div>
                </div>

Calling is a repetition, though it works by outputting the pagination but only by default i.e. 1,2 next or previous 1,2 and if i have more pages, there is no how I can get the paginate_links(); defined in archive-forum accept the page number.

Answer

You have taken the right steps; You need to complete it with function.php in your child template to force the posts_per_page.

Put this in your function.php

function modify_forum_query($query) {
     $query->set('posts_per_page', 7);
}
add_action('pre_get_posts', 'modify_forum_query');

You can do whatever you like here too

<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8" id="forum_index">
                    <?php
                    set_query_var('table_title', __translate( 'Discussion Groups', 'bbpress' ));
                    bbp_get_template_part( 'content', 'archive-forum' );
                    ?>
                    <div class="forum_pagination font-weight-bold">
                        <?php echo paginate_links(); ?>
                    </div>
                </div>

In your echo paginate_links();, you can array of parameters to override the defaults.

Leave a Reply

Your email address will not be published. Required fields are marked *