Skip to content

Instantly share code, notes, and snippets.

@micahwave
Created August 10, 2012 20:44
Show Gist options
  • Save micahwave/3317698 to your computer and use it in GitHub Desktop.
Save micahwave/3317698 to your computer and use it in GitHub Desktop.
guest author madness
/**
* Returns an array of contributors and stories based on passed tab.
*
*/
function time_get_contributors( $tab = 'all' ) {
global $wpdb, $wp_query;
$contributors = wp_cache_get( 'time_contributors_'.$tab, 'time_contributors_cache' );
if( !$contributors ) {
// select all guest authors, join posts by guest author id, group by guest author id
switch( $tab ) {
case 'time-100' :
$contributors = $wpdb->get_results(
"
SELECT g.*, p.ID as article_id, p.post_title as article_title
FROM $wpdb->posts AS g
LEFT JOIN $wpdb->postmeta AS pm ON pm.meta_value = g.ID
LEFT JOIN $wpdb->posts AS p ON pm.post_id = p.ID
LEFT JOIN $wpdb->postmeta AS pm2 ON pm2.post_id = g.ID
WHERE g.post_type = 'time_guest'
AND pm.meta_key = 'time_guest_author'
AND pm2.meta_key = 'time_guest_time100'
AND ( pm2.meta_value = 'on' OR pm2.meta_value = 1 )
ORDER BY p.post_date DESC
LIMIT 0, 100
"
);
break;
/*
case 'most-recent' :
$contributors = $wpdb->get_results(
"
SELECT g.*, p.ID as article_id, p.post_title as article_title
FROM $wpdb->posts AS g
LEFT JOIN $wpdb->postmeta AS pm ON pm.meta_value = g.ID
LEFT JOIN $wpdb->posts AS p ON pm.post_id = p.ID
WHERE g.post_type = 'time_guest'
AND pm.meta_key = 'time_guest_author'
ORDER BY p.post_date DESC
"
);
break;
*/
default :
// LEFT JOIN $wpdb->posts AS p ON pm.post_id = p.ID
// LEFT JOIN ( SELECT s.* FROM $wpdb->posts AS s ORDER BY s.post_date DESC ) AS p ON p.ID = pm.post_id
// LEFT JOIN ( SELECT sub.ID, sub.post_title, MAX(sub.post_date) FROM $wpdb->posts AS sub GROUP BY sub.ID ) AS p ON p.ID = pm.post_id
$contributors = $wpdb->get_results(
"
SELECT g.*, p.ID as article_id, p.post_title as article_title
FROM $wpdb->posts AS g
LEFT JOIN $wpdb->postmeta AS pm ON pm.meta_value = g.ID
LEFT JOIN $wpdb->posts AS p ON pm.post_id = p.ID
WHERE g.post_type = 'time_guest'
AND pm.meta_key = 'time_guest_author'
ORDER BY p.post_date DESC
LIMIT 0, 100
"
);
break;
}
// having trouble getting this order in query using GROUP BY or MAX(p.post_date)
$guest_ids = array();
$filtered_contributors = array();
foreach( $contributors as $c ) {
if( !in_array( $c->ID, $guest_ids ) ) {
$filtered_contributors[] = $c;
$guest_ids[] = $c->ID;
}
}
$contributors = $filtered_contributors;
// sort by last name
$names = array();
// see if the contributor has a last name field filled out,
// otherwise split the post title and assume the last name
foreach( $contributors as $c ) {
$last_name = get_post_meta( $c->ID, 'time_guest_lname', true );
if( empty( $last_name ) ) {
$parts = explode( ' ', $c->post_title );
$last_name = $parts[1];
}
$names[$last_name] = $c;
}
// sort them alphabetically
ksort( $names );
$contributors = array();
// this seems dumb
foreach( $names as $name ) {
$contributors[] = $name;
}
// set our cache
if( count( $contributors ) ) {
wp_cache_set( 'time_contributors_'.$tab, $contributors, 'time_contributors_cache', 3600 * 12 );
}
}
return $contributors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment