Last active
October 13, 2015 04:08
-
-
Save afragen/4137084 to your computer and use it in GitHub Desktop.
get custom user meta for email users plugin
This file contains 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 | |
add_action( admin_head, 'return_meta' ); | |
function return_meta() { | |
get_user_meta_field_data ( 'drmc-department' ); | |
get_user_meta_field_data ( 'drmc-department', 'er' ); | |
} | |
function get_users_by_meta_data( $meta_key, $meta_value ) { | |
// Query for users based on the meta data | |
$user_query = new WP_User_Query( | |
array( 'meta_key' => $meta_key, 'meta_value' => $meta_value ) | |
); | |
// Get the results from the query, returning the first user | |
$users = $user_query->get_results(); | |
return $users; | |
} // end get_users_by_meta_data | |
function get_user_meta_field_data( $user_meta_field, $user_meta_field_value=NULL ) { | |
$meta_field_values = array(); | |
$user_meta_field_values = array(); | |
foreach ( get_users_by_meta_data( $user_meta_field, $user_meta_field_value ) as $user ) { | |
$meta_field_values[] = get_user_meta( $user->ID, $user_meta_field ); | |
//get emails for specified custom user meta field value | |
if ( ! is_null( $user_meta_field_value ) ) { | |
$emails[$user->display_name] = $user->user_email; | |
} | |
} | |
//get values for custom user meta field and return array | |
if ( is_null( $user_meta_field_value ) ) { | |
foreach ( $meta_field_values as $meta_field_value ) { | |
if ( ! in_array( $meta_field_value[0], $user_meta_field_values ) ) { | |
$user_meta_field_values[] = $meta_field_value[0]; | |
} | |
} | |
rsort($user_meta_field_values); | |
$user_meta_field_values = array_filter($user_meta_field_values); | |
sort($user_meta_field_values); | |
print_r ( $user_meta_field_values ); | |
return $user_meta_field_values; | |
} | |
//return array of email addresses for specific custom user meta field value | |
if ( ! is_null( $user_meta_field_value ) ) { | |
print_r ($emails); | |
return $emails; | |
} | |
} |
I wrote this as an example for getting the Emails Users plugin to grab meta data and use it to create a group mailing based upon that. This has Ben done for Email Users v4.4.4
Take a look at one of my other gists for the plugin used by Email Users to set this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm looking for what (I think) this code does. How do you use it with the Email Users plugin? We have a store with customers and subsets of those will have the same pickup address for their orders. We want to be able to email those customers by pickup location (which is stored in a user_meta field).