Skip to:
Content
Pages
Categories
Search
Top
Bottom

Adding Custom View Shortcode


  • Twist360
    Participant

    @twist360

    Hi Guys,

    I am trying to have a page in WordPress which shows two sections:

    1) posts with 20 or more replies
    2) posts with 19 or less replies

    I have created a page with the following shortcodes:

    [bbp-single-view id="twentyplus_posts"]
    [bbp-single-view id="lesstwenty_posts"]

    I have added the following into my themes functions.php file:

    
    // Add Custom View - Forum Home
    
    add_action( 'bb_init', 'view_twentyplus_posts_init' );
    
    function view_twentyplus_posts_init() 
    {
    	$args = array( 'post_count' => '>19' );
    	bb_register_view( 'twentyplus_posts', __('Popular Posts', 'example'), $args, false );
    }
    
    add_action( 'bb_init', 'view_lesstwenty_posts_init' );
    
    function view_lesstwenty_posts_init() 
    {
    	$args = array( 'post_count' => '<20' );
    	bb_register_view( 'lesstwenty_posts', __('Forum Posts', 'example'), $args, false );
    }

    However all I get in response is:

    Oh bother! No topics were found here!

    Oh bother! No topics were found here!

    Any idea what I am doing wrong here?

Viewing 13 replies - 1 through 13 (of 13 total)

  • Robin W
    Moderator

    @robin-w

    Haven’t had a detailed look but

    bb_register_view should be bbp_register_view

    bb is buddypress, bbp is bbpress


    Twist360
    Participant

    @twist360

    Thats awesome, you are right, I also needed to make

    add_action( 'bb_init', 'view_twentyplus_posts_init' );

    become

    add_action( 'bbp_init', 'view_twentyplus_posts_init' );

    Need to play a little more as its not showing what I was expecting, but at least it’s showing something 🙂


    Robin W
    Moderator

    @robin-w

    great !


    Twist360
    Participant

    @twist360

    Hi Robin,

    Sadly the custom view is not actually showing posts with ( ‘post_count’ => ‘<20’ );

    Can you confirm ‘post_count’ is the correct value to pass over as it does not seem to work?

    Thanks

    Andy


    Robin W
    Moderator

    @robin-w

    try ‘_bbp_reply_count’ it’s in the meta data


    Twist360
    Participant

    @twist360

    Sadly not,

    add_action( 'bbp_init', 'view_twentyplus_posts_init' );
    
    function view_twentyplus_posts_init() 
    {
    	//$args = array( '_bbp_topic_reply_count' => '>20' );
    	
    	$args  = array( 
    	 '_bbp_reply_count' => '19'
    	);
    	
    	bbp_register_view( 'twentyplus_posts', __('Popular Posts', 'twentyplus'), $args, false );
    	
    }

    Shows me posts with no responses 🙁


    Robin W
    Moderator

    @robin-w

    your code says

    '_bbp_reply_count' => '19'

    shouldn’t that be

    '_bbp_reply_count' => '>19'


    Robin W
    Moderator

    @robin-w

    and since bbp_reply_count is in the metadata, you’d need to refer to that

    see

    https://codex.wordpress.org/Class_Reference/WP_Query

    so something like

    add_action( 'bbp_init', 'view_twentyplus_posts_init' );
    
    function view_twentyplus_posts_init() 
    {
    		
    	$args  = array( 
    
    	 'meta_key'   => '_bbp_reply_count' ,
              'orderby'  => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ),
              'meta_query' => array(
    		         array(
    			'key'     => '_bbp_reply_count' ,
    			'value'   => '19',
    			'compare' => '>',
    		),
    
    	);
    
    );
    
    	
    	bbp_register_view( 'twentyplus_posts', __('Popular Posts', 'twentyplus'), $args, false );
    	
    }
    
    

    Twist360
    Participant

    @twist360

    Thanks once again Robin, one final question, how do I have a second orderby for say freshness (_bbp_last_active_id).

    I would like to show topics > 19 replied but also order by latest reply.

    Been playing for hours and cannot crack this.

    Thank you.


    Robin W
    Moderator

    @robin-w

    see

    https://codex.wordpress.org/Class_Reference/WP_Query

    and look at the heading

    Order & Orderby Parameters

    my guess would be

    ‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘date’ => ‘desc’ ),

    `

    the date is the date of the post ie topic


    Robin W
    Moderator

    @robin-w

    sorry just reread your post and you want the latest reply !

    I’ll come back !


    Robin W
    Moderator

    @robin-w

    hmm, can’t see an easy way to do this (or any way in fact without spending hours which I don’t have) – suggest you carry on playing or decide that it’s not critical 🙂 🙂


    Robin W
    Moderator

    @robin-w

    @twist360 I’d like to save this solution in my folder of useful info.

    To save me copying and cobbling together the final answer from the above, can you post the final code you did for the solution below as one package

    This will help others and be useful

    Thanks Robin

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.
Skip to toolbar