Last active
February 2, 2016 20:43
-
-
Save stephanieleary/50bbdbd4acc0db54d45e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// * Search & Filter Pro filters | |
// Limit taxonomy term dropdowns to terms associated with the form's specified post type(s) | |
// cf https://gist.github.com/rmorse/7b59b45a14b1ca179868#file-sf-pro-filter-input-object-php | |
function example_filter_input_object( $input_object, $sfid ) { | |
if ( ( !in_array( $input_object['name'], array( '_sft_one', '_sft_two', '_sft_three' ) ) ) ) | |
return $input_object; | |
// make sure the options variable actually exists (it's only available to certain field types) | |
if ( !isset( $input_object['options'] ) ) | |
return $input_object; | |
if ( '_sft_one' == $input_object['name'] ) | |
$taxonomy = 'one'; | |
if ( '_sft_two' == $input_object['name'] ) | |
$taxonomy = 'two'; | |
if ( '_sft_three' == $input_object['name'] ) | |
$taxonomy = 'three'; | |
$meta_key = ''; | |
// get the taxonomy terms we want, then get all their ancestors (array of slugs) | |
$limited_terms = example_get_terms_by_post_type( $taxonomy, $post_type, $meta_key ); | |
if ( is_taxonomy_hierarchical( $taxonomy ) ) | |
$limited_terms = example_get_terms_parents( $limited_terms, $taxonomy ); | |
$new_options = $sticky_options = array(); | |
// set up terms that will stick to the top of the list | |
$sticky_slugs = array( 'online', 'all-states' ); | |
// get the "all items" option first | |
$sticky_options[] = array_shift( $input_object['options'] ); | |
foreach ( $input_object['options'] as $option ) { | |
if ( in_array( $option->value, $limited_terms ) ) { | |
if ( in_array( $option->value, $sticky_slugs ) ) | |
$sticky_options[] = $option; | |
else | |
$new_options[] = $option; | |
} | |
} | |
$new_options = array_merge( $sticky_options, $new_options ); | |
// TESTING | |
/* | |
if ( current_user_can('manage_options') ) { | |
//echo '<p>Original input object: </p>'; | |
//var_dump( $input_object ); | |
echo '<p>New options: </p>'; | |
var_dump( $new_options ); | |
echo '<p>Desired Terms: </p>'; | |
var_dump( $limited_terms ); | |
} | |
/**/ | |
// ditch all the old options | |
unset( $input_object['options'] ); | |
// replace with our new list | |
$input_object['options'] = $new_options; | |
return $input_object; | |
} | |
add_filter( 'sf_input_object_pre', 'example_filter_input_object', 10, 2 ); | |
// get an array of taxonomy term slugs associated with a specific post type (for shared taxonomies) | |
function example_get_terms_by_post_type( $taxonomies, $post_types, $meta_key = NULL ) { | |
$transient = 'limited_terms_' . $taxonomies . '_for_' . $post_types; | |
if ( !empty( $meta_key ) ) | |
$transient .= '_and_' . $meta_key; | |
$transient = sanitize_key( $transient ); | |
// Get any existing copy of our transient data | |
if ( false === ( $terms_for_post_type = get_transient( $transient ) ) ) { | |
global $wpdb; | |
$meta_join = $meta_and = ''; | |
if ( !empty( $meta_key ) ) { | |
$meta_join = "INNER JOIN $wpdb->postmeta AS pm ON pm.post_id = p.ID"; | |
$meta_and = sprintf( "AND pm.meta_key = '%s'", $meta_key ); | |
} | |
$query = $wpdb->prepare( | |
"SELECT t.slug from $wpdb->terms AS t | |
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id | |
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id | |
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id | |
$meta_join | |
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s') | |
$meta_and | |
GROUP BY t.term_id", | |
$post_types, | |
$taxonomies | |
); | |
$terms_for_post_type = $wpdb->get_col( stripslashes( $query ) ); | |
set_transient( $transient, $terms_for_post_type, HOUR_IN_SECONDS / 2 ); | |
} | |
return $terms_for_post_type; | |
} | |
// restore parent/child hierarchies after cherry-picking child terms | |
function example_get_terms_parents( $limited_terms, $taxonomy ) { | |
$parent_slugs = array(); | |
foreach ( $limited_terms as $child_term ) { | |
$term_obj = get_term_by( 'slug', $child_term, $taxonomy ); | |
$parent_ids = get_ancestors( $term_obj->term_id, $taxonomy ); | |
foreach ( $parent_ids as $id ) { | |
$parent_term = get_term_by( 'id', $id, $taxonomy ); | |
$parent_slugs[] = $parent_term->slug; | |
} | |
} | |
return array_merge( $limited_terms, $parent_slugs ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment