Skip to content

Instantly share code, notes, and snippets.

@carolinerusso
Created March 5, 2015 14:17
Show Gist options
  • Save carolinerusso/5041e981e249ec2f588a to your computer and use it in GitHub Desktop.
Save carolinerusso/5041e981e249ec2f588a to your computer and use it in GitHub Desktop.
Linking user_meta to taxonomy (and pulling user_meta data)
<?php
$queried_object = get_queried_object();
$term_name = $queried_object->name;
$users = get_users( array( 'meta_key' => 'school_name', 'meta_value' => $term_name, ) );
// users have user_meta stored that matches (exactly) the "school" and "sorority" taxonomies
// so let's get all users based on their CURRENT taxonomy association
// meaning, that if we are on taxonomy "School" > term "Harvard University"
// we'll get all users that have meta_key "school_name" AND meta_value "Harvard University"
if ( $users ) {
$bloggers = array(); // setup array 1
$school_chapters = array(); // setup array 2
$school_views = array(); // setup array 3
foreach( $users as $blogger ) {
// if the user has the role "contributor", the user is "active"
// only store and save values to these arrays if we are dealing with "contributors"
if ( $blogger->roles[0] == "contributor" ) {
// store array 1 = blogger array
$bloggers[] = array ( 'first_name' => $blogger->first_name, 'last_name' => $blogger->last_name, 'user_login' => $blogger->user_login, 'school_name' => $blogger->school_name, 'role' => $blogger->roles[0], 'id' => $blogger->ID );
// store array 2 = school_chapters array
$school_chapters[] = array ( 'chapter' => $blogger->chapter_name, 'school_name' => $blogger->school_name, 'school_slug' => $blogger->school_slug, 'sorority_name' => $blogger->sorority_name, 'sorority_slug' => $blogger->sorority_slug, 'role' => $blogger->roles[0] );
}
// we want to get view counts, though, for ALL users (including inactive "subscribers")
// so we'll remove if condition above that only looks for users with the contributor role
// store array 3 = school_views array
$school_views[] = array ( 'first_name' => $blogger->first_name, 'last_name' => $blogger->last_name, 'user_login' => $blogger->user_login, 'views' => $blogger->total_user_postviews, 'sorority_name' => $term_name );
}
}
// now we need to regroup and resort the data above so it's nice and oragnized for us to use!
// setup array $regrouped
$regrouped = array();
if ( $school_views ) { // if we have views (there must be articles for views)
foreach( $school_views as $top ){ // loop through all user
$regrouped[$top['school']] += $top['views']; // get 'views' key and add together
}
}
$school_views = $regrouped[$term_names]; // great, now everything is added and can be echoed out for a full count (echo $school_views for total school views count)
if ( !empty ( $school_chapters ) ) {
$school_chapters = array_map( "unserialize", array_unique( array_map( "serialize", $school_chapters )));
// use array_map to get array values and array_unique to dedupe based on key values
sort( $school_chapters );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment