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


お勧め

wp_print_inline_script_tag(2021年4月19日 更新)

void wp_print_inline_script_tag( string $javascript [ , array $attributes = array() ] )
インラインJavaScriptを含むscript要素を出力する。

register_rest_route(2022年8月17日 更新)

bool register_rest_route( string $namespace, string $route [ , array $args = array() [ , bool $override = false ] ] )
REST APIのルートを登録する。

wp_determine_option_autoload_value(2024年7月22日 更新)

string wp_determine_option_autoload_value( string $option, mixed $value, mixed $serialized_value, boo l |string $autoload )
自動ロードのトリガー値を取得する。

single_tag_title(2018年5月27日 更新)

string single_tag_title( [ string $prefix = '' [ , bool $display = true ] ] )
投稿タグアーカイブページの投稿タグ名を取得し、パラメータ$displayがtrueならば表示する。$displayがfalseの場合は、文字列として返す。

home_url(2023年3月31日 更新)

string home_url( [ string $path = '' [ , string $scheme = null ] ] )
現在のブログ(サイト)のホームURLを取得する。ホームURLは、管理者ページの「設定」-「一般」の「サイトのアドレス(URL)」のこと。