Filters
Filter logic can be split between OR and AND in both Drupal and Wordpress depending on the query used. Below are examples of such queries and the platforms they were built in for future reference.
Wordpress
Subtractive Filter (AND filter)
Example below comes from the Portent site's people filter. This filter behaves on 1 set of checkboxes, where each additional checkbox further filters results to ones where all options are applicable.
// $active_filters is an array of machine names
function cwd_base_get_people_results_message( $active_filters ) {
global $wp_query;
$total_posts = $wp_query->found_posts;
$total_pages = $wp_query->max_num_pages;
$per_page = get_query_var( 'posts_per_page' );
$page_num = get_query_var( 'paged' ) ?: 1;
// If there are no posts, return "no results message"
if ( !$total_posts ) {
return 'No Results Found';
}
$results_message = 'Showing ';
// If there are more than one page of results, show range for current page
if ( $total_pages > 1 ) {
// Low number is 1 more than previous page's high number (or 1 for first page)
$low_num = ($page_num - 1) * $per_page + 1;
// High number is (usually) number per page multiplied by number of pages
$potential_high_num = $page_num * $per_page;
// If calculated high number is higher than the total number, use total instead
$high_num = $potential_high_num < $total_posts ? $potential_high_num : $total_posts;
// Add range of posts shown on current page to results message
$results_message .= $low_num . '–' . $high_num . ' of ';
}
// Add total post count to results message
$results_message .= $total_posts;
// Add "people/person" if there are multiple results vs only one
$results_message .= $total_posts > 1 ? ' people' : ' person';
// If there are no active filters
if ( !$active_filters ) {
// Results message just indicates how many people there are in total
return $results_message;
}
// Generate human-readable list of active research area filters
$research_names = array();
// Convert machine-name list into readable text
foreach ( $active_filters as $key => $research ) {
// Convert dashes to spaces to get human readable name
$research_name = str_replace( '-', ' ', $research );
// Check if this is the last research area
$is_last = $key == array_key_last( $active_filters );
// If there are more than one research area, prepend '&' on last one
// (This makes it easier to join the filters into a single string later)
$research_prefix = count( $active_filters ) > 1 && $is_last ? '& ' : '';
array_push( $research_names, $research_prefix . $research_name );
}
// Separate two filters with spaces; separate three or more with commas
$research_join_char = count( $research_names ) > 2 ? ', ' : ' ';
// Combine the filters into a single string
$research_filters_string = implode( $research_join_char, $research_names );
// Adjust message based on if there are one or multiple results
$research_filters_prefix = $total_posts > 1 ? ' who research ' : ' who researches ';
// Add filter string to results message
$results_message .= $research_filters_prefix . $research_filters_string;
return $results_message;
}