この記事は最後に更新してから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


お勧め

is_ssl(2022年7月2日 更新)

bool is_ssl( )
リクエストがSSLかどうか調べる。

comments_template(2018年5月27日 更新)

void comments_template( string $file = '/comments.php' , bool $separate_comments = false )
投稿ページ(post)または単一ページ(page)のコメント情報を取得し、コメント表示・投稿用のテンプレートファイルを読み込んで表示する。

wp_list_pages(2015年4月28日 更新)

string wp_list_pages( [ mixed $args = '' ] )
固定ページを一覧表示する。

get_post_mime_type(2014年11月16日 更新)

mixed get_post_mime_type( [ mixed $ID = '' ] )
添付ファイルのMIMEタイプを取得する。

wp_lostpassword_url(2018年5月27日 更新)

string wp_lostpassword_url( [ string $redirect = '' ] )
パスワードリマインドURLを取得する。パスワードリマインドURLは、ログインページで「パスワードをお忘れですか?」のリンクとして使用されている。