Skip to:
Content
Pages
Categories
Search
Top
Bottom

Update pagination values


  • alexstine
    Participant

    @alexstine

    Hello,

    I’m using a plugin to integrate Amazon CloudSearch on my site. Whenever I exclude private topics from search results or private forums from search results, I might use some code like this.

    function exclude_private_topics_search() {
    	$topic_id = bbp_get_topic_id();
    	if(gdbbx_is_topic_private($topic_id) ) {
    		add_post_meta($topic_id, 'acs_exclude', 1, true );
    	} else {
    		delete_post_meta($topic_id, 'acs_exclude');
    	}
    }
    add_action('bbp_new_topic', 'exclude_private_topics_search' );
    add_action('bbp_edit_topic', 'exclude_private_topics_search' );
    
    function exclude_private_forums_search() {
    	$forum_id = bbp_get_forum_id();
    	if (get_post_status($forum_id) == 'private') {
    		add_post_meta($forum_id, 'acs_exclude', 1, true );
    	} else {
    		delete_post_meta($forum_id, 'acs_exclude');
    	}
    }
    add_action('bbp_new_forum', 'exclude_private_forums_search' );
    add_action('bbp_edit_forum', 'exclude_private_forums_search' );

    The problem is this does not update pagination counts. For example if 2 topics that are private are hidden and there are 5 topics in the forum, it will still display a total of 5 topics instead of 3 topics. How can I update the pagination values if there are less topics displaying AKA private ones?

    Thanks.

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

  • alexstine
    Participant

    @alexstine

    Hello,

    Any ideas on this one? I really have no ideas here.

    Thanks.


    Robin W
    Moderator

    @robin-w

    not quite sure what you are asking in using the word ‘pagination’ vs topic counts.

    But check that this is not just because you are using a keymaster role, so seeing keymaster counts. Hidden topics are not normally included for participants.

    if still an issue come back


    alexstine
    Participant

    @alexstine

    Hello @robin-w,

    I’m not using a default bbPress function to hide the topics. I’m using a custom plugin to handle the hiding of topics so members who are participants have a choice if they want the topic to be public to all members or private to moderators and keymasters. As you can see with the code above, that excludes the custom private topics from search results. Now what I need to do is update the pagination counts.

    For example, there’s a topic named “Testing” and another topic named “test123”. Pagination will show 2 topics. If “testing” is now marked private, pagination will show 2 topics. I need to figure out a way to update the pagination so it only shows 1 topic since that’s all is showing to participants. However if a moderator comes along with the required permission to view a private topic, the pagination should still show 2 topics since the moderator has permission to see the private topic.

    I hope this helps explain my goal.

    Thanks.


    Robin W
    Moderator

    @robin-w

    thanks for that, but still not sure on the use of the word ‘pagination’

    where and using what are these numbers being shown?


    alexstine
    Participant

    @alexstine

    Hello @robin-w,

    It’s this text at the top and bottom of single forums.

    “Viewing 4 topics – 1 through 4 (of 4 total)”

    Thanks.


    Robin W
    Moderator

    @robin-w

    ok, but what are you using to display this?


    alexstine
    Participant

    @alexstine

    Hello @robin-w,

    If I’m not mistaken, doesn’t this come with bbPress by default? Not sure what template it is actually in right now, but if you open up a single forum, you should see it. For example this URL.

    Plugins

    Thanks.


    Robin W
    Moderator

    @robin-w

    ok, so this is a search function – yes?

    If so, look at this code that I use with my private groups plugin to filter search results and see if you can make this work for you

    //this function filters to the bbp search function to allow only returns from allowed forums
    
    function pg_has_search_results( $args = '' ) {
    	
    	global $wp_rewrite;
    //start with code as per bbp search !
    	/** Defaults **************************************************************/
    
    	$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    
    	// Default query args
    	$default = array(
    		'post_type'           => $default_post_type,         // Forums, topics, and replies
    		'posts_per_page'      => bbp_get_replies_per_page(), // This many
    		'paged'               => bbp_get_paged(),            // On this page
    		'orderby'             => 'date',                     // Sorted by date
    		'order'               => 'DESC',                     // Most recent first
    		'ignore_sticky_posts' => true,                       // Stickies not supported
    		's'                   => bbp_get_search_terms(),     // This is a search
    	);
    
    	// What are the default allowed statuses (based on user caps)
    	if ( bbp_get_view_all() ) {
    
    		// Default view=all statuses
    		$post_statuses = array(
    			bbp_get_public_status_id(),
    			bbp_get_closed_status_id(),
    			bbp_get_spam_status_id(),
    			bbp_get_trash_status_id()
    		);
    
    		// Add support for private status
    		if ( current_user_can( 'read_private_topics' ) ) {
    			$post_statuses[] = bbp_get_private_status_id();
    		}
    
    		// Join post statuses together
    		$default['post_status'] = implode( ',', $post_statuses );
    
    	// Lean on the 'perm' query var value of 'readable' to provide statuses
    	} else {
    		$default['perm'] = 'readable';
    	}
    	
    	//PRIVATE GROUPS then loop to find allowable results
    	//bail from this part if there are no search terms, as otherwise it sorts the whole database and overflows memory
    	if (! bbp_get_search_terms() == '' ) {
    	//change page default to allow filter against all search results - otherwise allowed posts is only the first page of results ie whatever is in  bbp_get_replies_per_page()
    	$default['posts_per_page'] = -1;
    	$allowed_posts = private_groups_get_permitted_post_ids(new WP_Query( $default ));
    	// Then add allowed forum ids to the default query 
        $default['post__in'] = $allowed_posts;
    	if (empty ($allowed_posts )) $default['post__in'] = array(0) ;
    	//then set per page back (so that we get the correct pagination )
    	$default['posts_per_page'] = bbp_get_replies_per_page();
    	
    	}
    	
    	
    	
    	//then return to bbp search code
    	
    	/** Setup *****************************************************************/
    
    	// Parse arguments against default values
    	$r = bbp_parse_args( $args, $default, 'has_search_results' );
    
    	// Get bbPress
    	$bbp = bbpress();
    
    	// Call the query
    	if ( ! empty( $r['s'] ) ) {
    		$bbp->search_query = new WP_Query( $r );
    	}
    
    	// Add pagination values to query object
    	$bbp->search_query->posts_per_page = $r['posts_per_page'];
    	$bbp->search_query->paged          = $r['paged'];
    
    	// Never home, regardless of what parse_query says
    	$bbp->search_query->is_home        = false;
    
    	// Only add pagination is query returned results
    	if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) {
    
    		// Array of arguments to add after pagination links
    		$add_args = array();
    
    		// If pretty permalinks are enabled, make our pagination pretty
    		if ( $wp_rewrite->using_permalinks() ) {
    
    			// Shortcode territory
    			if ( is_page() || is_single() ) {
    				$base = trailingslashit( get_permalink() );
    
    			// Default search location
    			} else {
    				$base = trailingslashit( bbp_get_search_results_url() );
    			}
    
    			// Add pagination base
    			$base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
    
    		// Unpretty permalinks
    		} else {
    			$base = add_query_arg( 'paged', '%#%' );
    		}
    
    		// Add args
    		if ( bbp_get_view_all() ) {
    			$add_args['view'] = 'all';
    		}
    
    		// Add pagination to query object
    		$bbp->search_query->pagination_links = paginate_links(
    			apply_filters( 'bbp_search_results_pagination', array(
    				'base'      => $base,
    				'format'    => '',
    				'total'     => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ),
    				'current'   => (int) $bbp->search_query->paged,
    				'prev_text' => is_rtl() ? '→' : '←',
    				'next_text' => is_rtl() ? '←' : '→',
    				'mid_size'  => 1,
    				'add_args'  => $add_args, 
    			) )
    		);
    
    		// Remove first page from pagination
    		if ( $wp_rewrite->using_permalinks() ) {
    			$bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links );
    		} else {
    			$bbp->search_query->pagination_links = str_replace( '&paged=1', '', $bbp->search_query->pagination_links );
    		}
    	}
    	//finally filter to return
    	// Return object
    	return apply_filters( 'pg_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query );
    }
    
    add_filter ('bbp_has_search_results', 'pg_has_search_results') ; 

    alexstine
    Participant

    @alexstine

    Hello,

    I started to work on the code and then got my bill. That’s when I instantly stopped using CloudSearch. It’s a good starting place though. If I ever revisit CloudSearch in the future, I’m coming back for the code.

    Thanks for the help.

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