Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save VictorPietro/2475e65dc3c2080bc25ab260eb3a30b4 to your computer and use it in GitHub Desktop.

Select an option

Save VictorPietro/2475e65dc3c2080bc25ab260eb3a30b4 to your computer and use it in GitHub Desktop.
Filter Users by Exploded Meta Text Area Field (comma & semicolon)
/***
## Quick Setup & Usage Guide
1. **Add the Snippet**
* Copy both functions into your theme’s `functions.php` or a site‑specific plugin.
* Replace the Query Builder ID (`22`) with your actual JetEngine query ID.
2. **Configure JetEngine Query Builder**
* Create or edit the Query Builder that pulls the `user_publish-public-areas` meta.
* Ensure it returns each row as an object with a single `name` field containing terms separated by commas and/or semicolons.
3. **Configure JetSmartFilters**
* **Items Source:** select your JetEngine Query Builder (by its ID).
* **Query Variable:** set to `user_public_areas`.
* **Filter Type:** choose “Checkboxes” (or your preferred control) so each term appears as a separate option.
4. **How It Works**
* **Term Splitter:**
The first function hooks into `jet-engine/query-builder/query/items`, splits each `name` on commas **and** semicolons, deduplicates the raw terms, and outputs an array of `{ name }` objects for the filter UI.
* **Meta Filter:**
The second function hooks into `jet-smart-filters/query/final-query`, reads the selected `user_public_areas` values, builds an **OR**‑based `meta_query` that does a `LIKE` search on the `user_publish-public-areas` meta, and returns only matching users.
5. **Testing**
* Visit the page with your SmartFilter control: you should see one checkbox per unique term.
* Select any option(s) and verify that the user list updates to include only those whose `user_publish-public-areas` contains the selected term(s).
***/
<?php
/**
* -----------------------------------------------------------------------------
* 1) Split & Dedupe “Public Areas” Terms for JetEngine Query Builder Items
* -----------------------------------------------------------------------------
*/
/**
* Hook into JetEngine’s Query Builder items to:
* - split comma‑ and semicolon‑delimited strings,
* - remove duplicate terms,
* - and return an array of stdClass { name } objects.
*
* @param array $items Array of stdClass { name, … } from your SQL query.
* @param Base_Query $query The Query Builder instance (use $query->id to target).
* @return array New array of stdClass { name }.
*/
add_filter( 'jet-engine/query-builder/query/items', 'split_unique_publish_areas', 10, 2 );
function split_unique_publish_areas( $items, $query ) {
// Only target your specific Query Builder (replace 22 with your own ID)
if ( 22 !== (int) $query->id ) {
return $items;
}
$terms = [];
foreach ( $items as $item ) {
if ( empty( $item->name ) ) {
continue;
}
// 1) Split on commas AND semicolons
$parts = preg_split( '/[;,]+/', $item->name );
foreach ( $parts as $part ) {
$term = trim( $part );
if ( '' === $term ) {
continue;
}
// 2) Collect unique raw terms
$terms[ $term ] = true;
}
}
// 3) Build output array of stdClass { name }
$output = [];
foreach ( array_keys( $terms ) as $term ) {
$output[] = (object) [ 'name' => $term ];
}
return $output;
}
/**
* -----------------------------------------------------------------------------
* 2) Filter the User Listing via JetSmartFilters Using “Public Areas”
* -----------------------------------------------------------------------------
*/
/**
* Hook into JetSmartFilters’ final query to filter users by any of the
* selected “Public Areas” terms.
*/
add_filter( 'jet-smart-filters/query/final-query', 'filter_users_by_public_areas' );
/**
* Modifies the query args so that users are returned only if their
* usermeta 'user_publish-public-areas' contains any of the values
* selected in the Smart Filter (Query Variable = 'user_public_areas').
*
* @param array $query_args The args array before the query runs.
* @return array Modified args with a custom meta_query block.
*/
function filter_users_by_public_areas( $query_args ) {
// Bail if there's no meta_query to inspect
if ( empty( $query_args['meta_query'] ) || ! is_array( $query_args['meta_query'] ) ) {
return $query_args;
}
$raw_inputs = [];
// 1) Extract & remove the clause with key == 'user_public_areas'
foreach ( $query_args['meta_query'] as $idx => $clause ) {
if ( isset( $clause['key'] ) && 'user_public_areas' === $clause['key'] ) {
$raw_inputs[] = $clause['value'];
unset( $query_args['meta_query'][ $idx ] );
}
}
// Nothing selected? Return original args.
if ( empty( $raw_inputs ) ) {
return $query_args;
}
// 2) Flatten inputs (arrays or strings), merge, split on commas/semicolons,
// and sanitize each term.
$flat = [];
foreach ( $raw_inputs as $input ) {
if ( is_array( $input ) ) {
$flat = array_merge( $flat, $input );
} else {
$flat[] = $input;
}
}
$combined = implode( ';', $flat );
$areas = preg_split( '/[;,]+/', $combined );
$areas = array_filter( array_map( 'sanitize_text_field', $areas ) );
if ( empty( $areas ) ) {
return $query_args;
}
// 3) Build an OR‑based meta_query block for any matching term
$or_block = [ 'relation' => 'OR' ];
foreach ( $areas as $term ) {
$or_block[] = [
'key' => 'user_publish-public-areas',
'value' => $term,
'compare' => 'LIKE',
];
}
// 4) Append our custom block onto the existing meta_query
$query_args['meta_query'][] = $or_block;
return $query_args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment