BBP_Topics_Widget::widget( mixed $args = array(), array $instance = array() )

Displays the output, the topic list

Description

Parameters

$args

(Optional)

Default value: array()

$instance

(Optional)

Default value: array()

Source

File: bp-forums/common/widgets.php

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
public function widget( $args = array(), $instance = array() ) {
 
    // Get widget settings
    $settings = $this->parse_settings( $instance );
 
    // Typical WordPress filter
    $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
 
    // Forums filter
    $settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base );
 
    // How do we want to order our results?
    switch ( $settings['order_by'] ) {
 
        // Order by most recent replies
        case 'freshness' :
            $topics_query = array(
                'post_type'           => bbp_get_topic_post_type(),
                'post_parent'         => $settings['parent_forum'],
                'posts_per_page'      => (int) $settings['max_shown'],
                'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
                'ignore_sticky_posts' => true,
                'no_found_rows'       => true,
                'meta_key'            => '_bbp_last_active_time',
                'orderby'             => 'meta_value',
                'order'               => 'DESC',
            );
            break;
 
        // Order by total number of replies
        case 'popular' :
            $topics_query = array(
                'post_type'           => bbp_get_topic_post_type(),
                'post_parent'         => $settings['parent_forum'],
                'posts_per_page'      => (int) $settings['max_shown'],
                'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
                'ignore_sticky_posts' => true,
                'no_found_rows'       => true,
                'meta_key'            => '_bbp_reply_count',
                'orderby'             => 'meta_value',
                'order'               => 'DESC'
            );
            break;
 
        // Order by which topic was created most recently
        case 'newness' :
        default :
            $topics_query = array(
                'post_type'           => bbp_get_topic_post_type(),
                'post_parent'         => $settings['parent_forum'],
                'posts_per_page'      => (int) $settings['max_shown'],
                'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
                'ignore_sticky_posts' => true,
                'no_found_rows'       => true,
                'order'               => 'DESC'
            );
            break;
    }
 
    // Note: private and hidden forums will be excluded via the
    // bbp_pre_get_posts_normalize_forum_visibility action and function.
    $widget_query = new WP_Query( $topics_query );
 
    // Bail if no topics are found
    if ( ! $widget_query->have_posts() ) {
        return;
    }
 
    echo $args['before_widget'];
 
    if ( !empty( $settings['title'] ) ) {
        echo $args['before_title'] . $settings['title'] . $args['after_title'];
    } ?>
 
    <ul>
 
        <?php while ( $widget_query->have_posts() ) :
 
            $widget_query->the_post();
            $topic_id    = bbp_get_topic_id( $widget_query->post->ID );
            $author_link = '';
 
            // Maybe get the topic author
            if ( ! empty( $settings['show_user'] ) ) :
                $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) );
            endif; ?>
 
            <li>
                <a class="bbp-forum-title" href="<?php bbp_topic_permalink( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
 
                <?php if ( ! empty( $author_link ) ) : ?>
 
                    <?php printf( __( 'by %1$s', 'buddyboss' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
 
                <?php endif; ?>
 
                <?php if ( ! empty( $settings['show_date'] ) ) : ?>
 
                    <div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
 
                <?php endif; ?>
 
            </li>
 
        <?php endwhile; ?>
 
    </ul>
 
    <?php echo $args['after_widget'];
 
    // Reset the $post global
    wp_reset_postdata();
}

Changelog

Changelog
Version Description
bbPress (r2653) 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.