Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Last active July 9, 2016 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/e032c4103b0d6caf997439f1df3bcdbe to your computer and use it in GitHub Desktop.
Save helgatheviking/e032c4103b0d6caf997439f1df3bcdbe to your computer and use it in GitHub Desktop.
Add user taxonomy/term arguments to Simple User Listing
<?php
/**
* Plugin Name: Simple User Listing Taxonomy Support
* Plugin URI: https://gist.github.com/helgatheviking/e032c4103b0d6caf997439f1df3bcdbe
* Description: add a taxonomy query to users
* Version: 1.0.0b
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com/
*
* Copyright: © 2016 Kathy Darling
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/*
* Will require Simple User Listings > 1.7.3
*
* Usage: upload and activate this plugin
* register_taxonomy() for user taxonomies, see: For User Taxonomies, see: http://bit.ly/1p50xkr
* use [userlist taxonomy="profession" terms="athlete"]
* or [userlist taxonomy="profession" terms="athlete|developer"]
*/
/**
* Add a "tax_query" to the query args
* @param array $args
* @param str $query_id
* @param array $atts - original shortcode attributes
* @return array
*/
function kia_user_taxonomy_args( $args, $query_id, $atts ){
// taxonomy params, this is not supported by WP_User_Query, but we're faking it with pre_user_query
if ( $atts['taxonomy'] && $atts['terms'] ) {
$terms = explode("|", $atts['terms'] );
$args['tax_query'] = array(
array(
'taxonomy' => $atts['taxonomy'],
'field' => 'slug',
'terms' => $terms,
),
);
}
return $args;
}
add_filter( 'sul_user_query_args', 'kia_user_taxonomy_args', 10, 3 );
/**
* Fake a "tax_query"
* @param obj $query - by reference
* @param str $query_id
*/
function kia_user_taxonomy_query( $query ) {
global $wpdb;
// fake a tax query
if ( isset( $query->query_vars['tax_query'] ) && is_array( $query->query_vars['tax_query'] ) ) {
$sql = get_tax_sql( $query->query_vars['tax_query'], $wpdb->prefix . 'users', 'ID' );
if( isset( $sql['join'] ) ){
$query->query_from .= $sql['join'];
}
if( isset( $sql['where'] ) ){
$query->query_where .= $sql['where'];
}
}
}
add_action( 'pre_user_query', 'kia_user_taxonomy_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment