Skip to content

Commit b922eda

Browse files
committed
Filter (multi)select fields for post types and taxonomies.
- When printing the value. - When editing the selected option(s). See #8
1 parent 475fa21 commit b922eda

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

src/field-types/class-field-type-multi-select-post-type.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public function edit_field_options_html( array $args = array() ) {
9696
$new_posts_selected = $_POST[ 'field_' . $this->field_obj->id ];
9797
$posts_selected = ( $posts_selected != $new_posts_selected ) ? $new_posts_selected : $posts_selected;
9898
}
99+
100+
$posts_selected = apply_filters( 'bp_get_the_profile_field_multiselect_custom_post_type_selected', $posts_selected, $args, $post_type_selected, $this->field_obj->id );
101+
99102
// Get posts of custom post type selected.
100103
$posts = new \WP_Query(
101104
array(
@@ -237,6 +240,7 @@ public static function display_filter( $field_value, $field_id = 0 ) {
237240

238241
$post_ids = explode( ',', $field_value );
239242
$post_ids = wp_parse_id_list( $post_ids );
243+
$post_ids = apply_filters( 'bp_xprofile_field_multiselect_custom_post_type_value', $post_ids, $field_id );
240244
_prime_post_caches( $post_ids, false, false );
241245

242246
$list = '';

src/field-types/class-field-type-multi-select-taxonomy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public function edit_field_options_html( array $args = array() ) {
9696
$terms_selected = ( $terms_selected != $new_terms_selected ) ? $new_terms_selected : $terms_selected;
9797
}
9898

99+
$terms_selected = apply_filters( 'bp_get_the_profile_field_multiselect_custom_taxonomy_selected', $terms_selected, $args, $taxonomy_selected, $this->field_obj->id );
100+
99101
// Get terms of custom taxonomy selected.
100102
$terms = get_terms( $taxonomy_selected, array( 'hide_empty' => false ) );
101103

@@ -229,6 +231,7 @@ public static function display_filter( $field_value, $field_id = '' ) {
229231

230232
$field_value = explode( ',', $field_value );
231233
$term_ids = wp_parse_id_list( $field_value );
234+
$term_ids = apply_filters( 'bp_xprofile_field_multiselect_custom_taxonomy_value', $term_ids, $field_id );
232235

233236
$tax = self::get_selected_taxonomy( $field_id );
234237
$list = '';

src/field-types/class-field-type-select-post-type.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public function edit_field_options_html( array $args = array() ) {
8686
$new_post_selected = (int) $_POST[ 'field_' . $this->field_obj->id ];
8787
$post_selected = ( $post_selected != $new_post_selected ) ? $new_post_selected : $post_selected;
8888
}
89+
90+
$post_selected = apply_filters( 'bp_get_the_profile_field_select_custom_post_type_selected', $post_selected, $args, $post_type_selected, $this->field_obj->id );
91+
8992
// Get posts of custom post type selected.
9093
$posts = new \WP_Query(
9194
array(
@@ -194,6 +197,8 @@ public static function display_filter( $field_value, $field_id = '' ) {
194197
return '';
195198
}
196199

200+
$post_id = apply_filters( 'bp_xprofile_field_select_custom_post_type_value', $post_id, $field_id );
201+
197202
return sprintf( '<a href="%1$s">%2$s</a>', esc_url( get_permalink( $post_id ) ), get_the_title( $post_id ) );
198203
}
199204

src/field-types/class-field-type-select-taxonomy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public function edit_field_options_html( array $args = array() ) {
8989
$term_selected = ( $term_selected != $new_term_selected ) ? $new_term_selected : $term_selected;
9090
}
9191

92+
$term_selected = apply_filters( 'bp_get_the_profile_field_select_custom_taxonomy_selected', $term_selected, $args, $taxonomy_selected, $this->field_obj->id );
93+
9294
// Get terms of custom taxonomy selected.
9395
$terms = get_terms( $taxonomy_selected, array( 'hide_empty' => false ) );
9496

@@ -205,6 +207,8 @@ public static function display_filter( $field_value, $field_id = 0 ) {
205207
$term_id = absint( $field_value );
206208
$tax = self::get_selected_taxonomy( $field_id );
207209

210+
$term_id = apply_filters( 'bp_xprofile_field_select_custom_taxonomy_value', $term_id, $field_id );
211+
208212
$term = get_term( $term_id, $tax );
209213
if ( ! $term || is_wp_error( $term ) ) {
210214
return '';

0 commit comments

Comments
 (0)