この記事は最後に更新してから1年以上経過しています。

4.2の「アクティビティ」ウィジェットに投稿以外の情報も表示する

説明

ダッシュボードの「アクティビティ」ウィジェットは「投稿(post)」の状況を表示し、固定ページ(page)やカスタム投稿タイプの投稿記事の状況については標準では表示されない。このことに対して以前『3.8の「アクティビティ」ウィジェットに投稿以外の情報も表示する』を紹介したのだが、4.2では新しいフィルターが追加され、カスタマイズが少し容易になった。

ダッシュボードの「アクティビティ」ウィジェットには、「公開間近」として予約登録されている投稿情報が、「最近公開」として公開済みの新しい投稿情報がリスト表示される。これらの情報は、先のコラムで記載した通り/wp-admin/includes/dashboard.phpで定義されているwp_dashboard_recent_posts関数でWP_Queryクラスを使って取得しており、4.2ではこの関数内にパラメータ内容を調整できる'dashboard_recent_posts_query_args'フィルターが追加された。

$query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args );

「公開間近」と「最近公開」時の$query_argsの内容は次の通り。

連想配列キー「公開間近」時の内容「最近公開」時の内容
'post_type'"post""post"
'post_status'"future""publish"
'orderby'"date""date"
'order'"ASC""DESC"
'posts_per_page'55
'no_found_rows'truetrue
'cache_results'falsefalse
'perm'"editable""readable"

どちらの呼び出しも'post_type'は"post"なので、ここに表示したい投稿タイプを追加すれば当初の目的を果たすことができる。例えば、投稿と固定ページの両方を表示するなら、次の内容をテーマ内のfunctions.phpに記述すればよい。

add_filter( 'dashboard_recent_posts_query_args', 'my_dashboard_recent_posts_query_args', 10, 1 );
function my_dashboard_recent_posts_query_args( $query_args ) {
	$query_args['post_type'] = array( 'post', 'page' );
	return $query_args;
}

また「最近公開」時の表示件数を最大10件に変更する場合は、'post_status'が"publish"の場合のみ'posts_per_page'を10に変更する。

add_filter( 'dashboard_recent_posts_query_args', 'my_dashboard_recent_posts_query_args', 10, 1 );
function my_dashboard_recent_posts_query_args( $query_args ) {
	$query_args['post_type'] = array( 'post', 'page' );
	if ( $query_args['post_status'] == "publish" )
		$query_args['posts_per_page'] = 10;
	return $query_args;
}

WP_Queryクラスの'pre_get_posts'フィルターを利用する場合は呼び出し場所の判定が必要だったが、この'dashboard_recent_posts_query_args'フィルターでは呼び出し場所は限定されているので、パラメータを変更するためのコードを記述すればよい。


最終更新 : 2018年05月27日 10:45


お勧め

count_users(2019年3月1日 更新)

array count_users( [ string $strategy = 'time' [, int $site_id = null ] ] )
役割別のユーザ数を取得する。

wp_embed_defaults(2011年12月1日 更新)

array wp_embed_defaults( )
投稿記事中に埋め込む動画などのサイズを取得する。

the_posts_navigation(2019年9月25日 更新)

void the_posts_navigation( [ array $args = array() ] )
アーカイブページ向けのナビゲーションを表示する。

show_admin_bar(2012年1月5日 更新)

void show_admin_bar( bool $show )
ツールバー(admin bar)を強制的に表示/非表示する。

wp_spam_comment(2013年9月6日 更新)

bool wp_spam_comment( $comment_id )
コメントをスパムにする。