• Resolved jmcallister

    (@jmcallister)


    We would like to exclude certain posts in certain categories from appearing in the search. Is that possible?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support BuddyBoss Support

    (@bbwpsupport)

    @jmcallister

    Absolutely! You could use filter BBoss_Global_Search_Posts_sql to modify posts search SQL query to exclude certain posts of certain categories.

    Thread Starter jmcallister

    (@jmcallister)

    Can you be more specific? What would the code look like if I wanted to exclude all posts from category 1? Where would the code go? Thanks!

    I see this bit in class.BBoss_Global_Search_Posts.php

    return apply_filters(
    ‘BBoss_Global_Search_Posts_sql’,
    $sql,
    array(
    ‘search_term’ => $search_term,
    ‘only_totalrow_count’ => $only_totalrow_count,
    )

    Thread Starter jmcallister

    (@jmcallister)

    @bbwpsupport, please advise.

    Thread Starter jmcallister

    (@jmcallister)

    Hello?

    • This reply was modified 6 years, 2 months ago by jmcallister.
    Plugin Support BuddyBoss Support

    (@bbwpsupport)

    Hey @jmcallister

    I’m really sorry for the delay here.

    When following code would be pasted in child theme’s functions.php, BuddyPress Global Search plugin will exclude all posts from category id 1 from the search result.

    add_filter( 'BBoss_Global_Search_Posts_sql', 'Custom_BBoss_Global_Search_Posts_sql', 10, 2 );
    
    function Custom_BBoss_Global_Search_Posts_sql( $sql, $args ) {
    
    			global $wpdb;
    			$query_placeholder = array(); 
    			
    			$sql = " SELECT ";
    			
    			if( $args['only_totalrow_count'] ){
    				$sql .= " COUNT( DISTINCT id ) ";
    			} else {
    				$sql .= " DISTINCT id , %s as type, post_title LIKE %s AS relevance, post_date as entry_date  ";
    				$query_placeholder[] = 'posts';
    				$query_placeholder[] = '%'. $args['search_term'] .'%';
    			}
    
    			$sql .= " FROM {$wpdb->posts} p ";
    
    			/* ++++++++++++++++++++++++++++++++
    			 * wp_posts table fields
    			 +++++++++++++++++++++++++++++++ */
    			$items_to_search = buddyboss_global_search()->option('items-to-search');
    			$post_fields 	 = array();
    			$tax 			 = array();
    			foreach( $items_to_search as $item ) {
    
    				//see print_search_options
    				if( strpos( $item, 'post_field_' )===0 ){
    					$post_field = str_replace( 'post_field_', '', $item );
    					$post_fields[$post_field] = true;
    				}
    
    				if ( strpos( $item, '-tax-' ) ) {
    					$tax[] = str_replace( 'posts-tax-', '', $item );
    				}
    			}
    
    			//Tax query left join
    			if ( ! empty( $tax ) ) {
    				$sql .= " LEFT JOIN {$wpdb->term_relationships} r ON p.ID = r.object_id ";
    			}
    
    			//WHERE
    			$sql .= ' WHERE 1=1 AND ( p.post_title LIKE %s OR p.post_content LIKE %s ';
    			$query_placeholder[] = '%'. $args['search_term'] .'%';
    			$query_placeholder[] = '%'. $args['search_term'] .'%';
    
    			//Tax query
    			if ( ! empty( $tax ) ) {
    
    				$tax_in_arr = array_map( function( $t_name ) {
    					return "'" . $t_name . "'";
    				}, $tax );
    
    				$tax_in = implode( ', ', $tax_in_arr );
    
    				$sql .= " OR  r.term_taxonomy_id IN (SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} tt INNER JOIN {$wpdb->terms} t ON 
    					  t.term_id = tt.term_id WHERE ( t.slug LIKE %s OR t.name LIKE %s ) AND  tt.taxonomy IN ({$tax_in}) )";
    					$query_placeholder[] = '%'. $args['search_term'] .'%';
    					$query_placeholder[] = '%'. $args['search_term'] .'%';
    			}
    
    			//Meta query
    			if ( ! empty( $post_fields['post_meta'] ) ) {
    				$sql .= " OR p.ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE %s )";
    				$query_placeholder[] = '%'. $args['search_term'] .'%';
    			}
    
    			//Post should be publish
    			$sql .= ") AND p.post_type = 'post' AND p.post_status = 'publish' AND p.ID NOT IN (
    			SELECT tr.object_id FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    			INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id WHERE t.term_id = 1 AND tt.taxonomy = 'category'
    			) ";
    
    			$sql = $wpdb->prepare( $sql, $query_placeholder );
    
    			return $sql;
    
    }
    Thread Starter jmcallister

    (@jmcallister)

    Awesome! This appears to be working great. Thank you so much! I will mark this resolved after I’ve confirmed.

    Thread Starter jmcallister

    (@jmcallister)

    Working great! Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Exclude Category from Search’ is closed to new replies.