WordPressでユーザーを投稿の新しい順に並べ、○件ずつ投稿を表示する

functions.php

function sorted_author_posts( $authors, $posts_num ) {
	$all_posts = array();
	$sort_date = array();
	$posts_num = (int)$posts_num;
	foreach ( (array)$authors as $author ) {
		$author = (int)$author;
		$author_data = get_userdata( $author );
		$author_posts = get_posts( "posts_per_page=$posts_num&author=$author" );
		if ( $author_posts ) {
			$all_posts[$author_data->display_name] = $author_posts;
			$sort_date[$author_data->display_name] = $author_posts[0]->post_date;
		}
	}

	array_multisort( $sort_date, $all_posts );
	$all_posts = array_reverse( $all_posts );
	return $all_posts;
}

テンプレートの記述例

<?php $users = array( 1, 2 );
$users_posts = sorted_author_posts( $users, 2 );
foreach ( $users_posts as $user_name => $user_posts ) : ?>
	<h2><?php echo $user_name ?></h2>
	<ul>
<?php foreach ( $user_posts as $post ) : setup_postdata( $post ); ?>
		<li><?php the_title(); ?></li>
<?php endforeach; ?>
	</ul>
<?php endforeach; wp_reset_postdata(); ?>

WordPressでユーザーの最新投稿日を取得する

functions.php

function get_author_latest_update( $author_id, $date_format = null ) {
	$latest_post = get_posts( 'posts_per_page=1&author=' . (int)$author_id );
	$update_date = '';
	if ( $latest_post ) {
		$format = is_null( $date_format ) ? get_option( 'date_format' ) : $date_format;
		$update_date = mysql2date( $format, $latest_post[0]->post_date );
	}
	return $update_date;
}

テンプレートファイル

<?php echo get_author_latest_update( 1 ); ?>

表示はこうなる

2012年2月28日

WordPressの日付別アーカイブのtitleタグに年やら日やらを追加する

WordPressの日付別アーカイブのtitleタグが「2012 3月 15」みたいに中途半端な表示になっているのが気になったので調整してみることにしました。

こんな感じのやつね。
修正前

<title>  2012  2月  28</title>

こんな感じで、うまく行くはず。。。

function jp_date_archive_wp_title( $title ) {
	$title = trim( $title );
	if ( is_date() ) {
		$replaces = array(
			'/([1-9]{1}[0-9]{3})/' => '$1年',
			'/ ([0-9]{1,2}) /'     => ' $1日 ',
			'/ ([0-9]{1,2})$/'     => ' $1日',
			'/[\s]+/'              => ' '
		);
		$title = preg_replace( array_keys( $replaces ), $replaces, $title );
	}
	return $title;
}
add_filter( 'wp_title', 'jp_date_archive_wp_title', 10 );

修正後

<title>2012年 2月 28日</title>

wp_titleのパラメーターでうまくいかない場合があったら、教えてください。