Skip to content

Instantly share code, notes, and snippets.

@thenbrent thenbrent/gist:8aca175136c79529c416 Secret
Last active Dec 15, 2015

Embed
What would you like to do?
Assorted taxonomy filter functions
<?php
/**
* Updates the count property of an array of term objects to reflect the number of posts in the
* current query that have the term.
*
* WordPress's default count comes from all posts in the DB, not the current query.
*
* @author Brent Shepherd <brent@findingsimple.com>
* @since 1.0
*/
function eg_update_term_counts( $terms, $taxonomy ) {
$post_ids = eex_get_all_post_ids_in_query();
if( empty( $post_ids ) )
return $terms;
// Get all the term IDs that apply to the current query's posts
$object_terms = wp_get_object_terms( $post_ids, $taxonomy, array( 'fields' => 'ids' ) );
$term_counts = array_count_values( $object_terms );
// Update the counts for each term
foreach( $terms as $key => $term )
if ( isset( $term_counts[$term->term_id] ) )
$term->count = $term_counts[$term->term_id];
else
unset( $terms[$key] );
return $terms;
}
/**
* Returns a search URI with the current filters.
*
* If the $filter parameter is set with $value parameter in the current query, then it is
* removed from the return URI, otherwise it is added.
*
* @param $filter string The filter's rewrite slug. eg. post_type
* @param $value string The value for the filter eg. event
*
* @author Brent Shepherd <brent@findingsimple.com>
* @since 1.0
*/
function eg_get_search_filter_uri( $filter, $value ) {
global $wp_query;
$search_link = ( empty( $wp_query->query_vars['s'] ) || $wp_query->query_vars['s'] == '~' ) ? get_search_link( '~' ) : get_search_link();
$search_link = add_query_arg( $_GET, $search_link );
$filter_query = '';
if( isset( $wp_query->query_vars[$filter] ) ) { // Results already being filtered by this filter type
$taxonomies = get_taxonomies( array( 'query_var' => $filter ) );
// If we are filtering by a post_type, replace it with the new post type
if( 'post_type' == $filter ) {
$filter_query = ( $wp_query->query_vars[$filter] == $value ) ? remove_query_arg( $filter, $search_link ) : add_query_arg( $filter, $value, $search_link );
// If we are filtering by taxonomy
} elseif( ! empty( $taxonomies ) ) {
// Loop over the taxonomies being used to filter
foreach( $wp_query->tax_query->queries as $query ) {
$tax = get_taxonomy( $query['taxonomy'] );
// If this taxonomy is not the filter in question, ignore it
if( $tax->query_var != $filter )
continue;
// If the term for this URL is not already being used to filter the taxonomy
if( strpos( $wp_query->query_vars[$filter], $value ) === false ) {
// Append the term to the taxonomy filter string
$filter_query = add_query_arg( $filter, $wp_query->query_vars[$filter] . '+' . $value, $search_link );
} else {
// Otherwise, remove the term
if( $wp_query->query_vars[$filter] == $value ) {
$filter_query = remove_query_arg( $filter, $search_link );
} else {
$filter_value = str_replace( $value, '', $wp_query->query_vars[$filter] );
// Remove any residual + symbols left behind
$filter_value = str_replace( '++', '+', $filter_value );
$filter_value = preg_replace( '/(^\+|\+$)/', '', $filter_value );
$filter_query = add_query_arg( $filter, $filter_value, $search_link );
}
}
}
}
} else {
$filter_query = add_query_arg( $filter, $value, $search_link );
}
return $filter_query;
}
/**
* Sets the count property of an array of post type objects to reflect the number of posts
* of that type in the current query. If a post type is not in the current query, it is
* removed from the $post_types array.
*
* @author Brent Shepherd <brent@findingsimple.com>
* @since 1.0
*/
function eg_get_post_types_with_counts( $post_type_names = array() ) {
global $wp_query;
if( empty( $post_type_names ) )
$post_type_names = array(
'eex_opportunity',
'eex_case_study',
'eex_event',
'eex_resources',
'eex_contact',
'eex_program',
'page'
);
foreach( $post_type_names as $post_type )
$post_types[$post_type] = get_post_type_object( $post_type );
$post_counts = array();
$post_ids = eex_get_all_post_ids_in_query( 'any' );
// Count the posts of each type
foreach( $post_ids as $post_id ) {
$post = get_post( $post_id );
if( ! in_array( $post->post_type, $post_type_names ) )
continue;
$post_counts[$post->post_type] = ( ! isset( $post_counts[$post->post_type] ) ) ? 1 : ++$post_counts[$post->post_type];
}
// If all post type counts are 0, calculate total post type counts and return full post types array
if( array_sum( $post_counts ) == 0 ) {
foreach( $post_types as $post_type_name => $post_type )
$post_types[$post_type_name]->count = wp_count_posts( $post_type_name )->publish;
} else {
foreach( $post_counts as $post_type_name => $count )
$post_types[$post_type_name]->count = $count;
}
// Remove any post types with a count of 0
foreach( $post_types as $post_type_name => $post_type )
if ( !isset( $post_type->count ) || $post_type->count == 0 )
unset( $post_types[$post_type_name] );
return $post_types;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.