Wordpress: Author List by Category 01Jul08 | 0

I just keep functioning out WP! (What can I say, it’s my job!)

This is a rework of WP’s own list_authors() function, but with some extra SQL:


<?php

function my_list_authors_by_category($args = '', $cat_id=0) {
global $wpdb;

$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true
);

$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);

$return = '';

/** @todo Move select to get_authors(). */
//    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
//replace with the 'author by category' SQL
$authors = $wpdb->get_results("SELECT distinct post_author as `ID`, user_nicename FROM `default_posts` as `dp`, `default_terms` as `dt`, `default_term_relationships` as `dtr`,`default_users` as `du` where `dp`.`post_author`=`du`.`ID` and `dp`.`ID`=`dtr`.`object_id` and `dtr`.`term_taxonomy_id`=".$cat_id.";");

$author_count = array();
foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
$author_count[$row->post_author] = $row->count;
}

foreach ( (array) $authors as $author ) {
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
$name = $author->display_name;

if ( $show_fullname && ($author->first_name != ” && $author->last_name != ”) )
$name = "$author->first_name $author->last_name";

if ( !($posts == 0 && $hide_empty) )
$return .= ‘<li>’;
if ( $posts == 0 ) {
if ( !$hide_empty )
$link = $name;
} else {
$link = ‘<a href="’ . get_author_posts_url($author->ID, $author->user_nicename) . ‘" title="’ . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . ‘">’ . $name . ‘</a>’;

if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ‘ ‘;
if (empty($feed_image))
$link .= ‘(’;
$link .= ‘<a href="’ . get_author_rss_link(0, $author->ID, $author->user_nicename) . ‘"’;

if ( !empty($feed) ) {
$title = ‘ title="’ . $feed . ‘"’;
$alt = ‘ alt="’ . $feed . ‘"’;
$name = $feed;
$link .= $title;
}

$link .= ‘>’;

if ( !empty($feed_image) )
$link .= "<img src=\"$feed_image\" style=\"border: none;\"$alt$title" . ‘ />’;
else
$link .= $name;

$link .= ‘</a>’;

if ( empty($feed_image) )
$link .= ‘)’;
}

if ( $optioncount )
$link .= ‘ (’. $posts . ‘)’;

}

if ( !($posts == 0 && $hide_empty) )
$return .= $link . ‘</li>’;
}
if ( !$echo )
return $return;
echo $return;
}
?>

And then to call, just define the category you want:


<?php

$cat=0;
if (have_posts()){    //get the present category, if there is one
if (is_category()) {
$cat = get_query_var('cat');
}
}
if($cat!=0){
?>
<h2>Authors:</h2>
<ul>
<? my_list_authors_by_category('exclude_admin=1',$cat); ?>
</ul>
<? } ?>

Sort Wordpress Posts by Custom Field 23Jun08 | 0

I do *A LOT* of query_posts in my WP Theming for work, and I’ve found a million-and-one reasons why I needed this bit-o’-code lately. Instead of having oh-so-blog-centric sortable options such as “By Title”, “By Category” “By Modified Time”, I needed to manually sort the posts in a given category. Why? Cuz I’ve got a list of posts in a category that needs to be sequential. Very non-bloggy, but VERY CMSy.

Solution: Just add this code in to each template & make sure the blog posts are in the category MYCATNAME with a custom tag of “order”, from 0 to ad infinitum.

<?php $posts=query_posts("category_name=OptionsPage&orderby=menu_order&order=asc");

$out_array=array();

//loop #1: get the post & attach post_meta 'order' to the menu_order
foreach($posts as $post){
$this_order=0;
if( strcmp(get_post_meta($post->ID, "order", true),"")!=0){
$this_order=get_post_meta($post->ID, "order", true);
}
$post->menu_order=$this_order;
if($out_array[$this_order]==null){//start the ordering  (solved: if 2 have the same order!)
$out_array[$this_order]=$post;
} else {
array_push($out_array,$post);
}
}

//sort acc.to menu_order
$wp_query->posts=null;
$wp_query->posts=array();
for($i=0;$i<count($out_array)+1;$i++){
if($out_array[$i]!=null){
array_push($wp_query->posts,$out_array[$i]);
}
}

//start looping through!
$idx=0;
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); $idx++;?>
<li class="flipmenuitem"><a id="link<? echo $idx; ?>" href="#" style="background:#FFF url(’<? echo bloginfo(’template_directory’);?>/images/<?if($idx==1){?>minus<?} else {?>plus<?}?>.gif’) no-repeat;padding-left:15px;" onclick="javascript:Effect.comboEffect0(’flip<? echo $idx; ?>’,this);"><?php the_title(); ?></a>
<div class="flippage" style="display:<?if($idx==1){?>block<?} else {?>none<?}?>;" id="flip<?echo $idx; ?>"><?php the_content(); ?></div></li>
<?php endwhile;
else :
endif; ?>
// … you get the picture here..

Information Errors 18Jun08 | 0

InfoWorld has a great write-up about a data-center project & what they learned from it. I’ve summarized and abstracted it here:

When you don’t know, you trust.
Differing experiences & personalities will trust different things:
D: themselves
I:  people: what they can get others to do/say.
S: what others say/face-value?
C: the information: their mind/face-value of what others say. They will not try to influence or change.

Details always trip you up
:
Practical: some times things just don’t work.
–Execution problem
Idealism: some times you just didn’t think or plan enough (and you could have).
–Information problem

Consequences:
Think + Do everyone’s job in the process, from vendor information, estimations, ordering, manufacturing, shipping; environmental surroundings; temporal surroundings (future-proofing)

Information Errors can be minimized by having a diverse enough team that can think through each step, and communicate (listen!) enough to take each info-tid-bit to heart. Most personalities aren’t capable of valuing this, and those that are, aren’t usually forceful or influential enough to make their case known. Solution? Humility.

| 0


| 0


| 0


| 0


| 0


| 0


| 0