10
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

《Wordpress》管理画面投稿一覧の項目を増やしてソートするカスタマイズ(タクソノミ/アイキャッチ/ID/カスタムフィールド)

Posted at

「Wordpress」の管理画面のカスタマイズです。
投稿一覧にいろいろな項目を追加して、ソート機能を実装します。

投稿タイプに応じて切り分けが可能で、追加した後も「表示オプション」を利用して簡単に非表示にすることもできます。


/**
 * 投稿一覧の項目をカスタマイズ
 *
*/

/**
* テーブルヘッダー部分のセット
* 投稿一覧共通
*/
function add_posts_columns_custom($columns) {

  // 右辺に一覧に表示させたい文字列
  $columns['thumbnail'] = '一覧画像'; // アイキャッチ
  $columns['postid'] = 'ID'; // 記事ID
  return $columns;

}

/**
* カスタム投稿タイプ《column》のみ
*/
function add_posts_columns_custom_column($columns) {

  $columns['writer'] = '執筆者';  // タクソノミー writer
  $columns['column-rank'] = '順位'; // カスタムフィールド colum-rank
  $columns['column-pickup'] = '注目コラム';
  $columns['column-score-num'] = '平均点'; // カスタムフィールドを演算する
  return $columns;

}

/**
* テーブルボディ部分のセット
* 投稿一覧共通
*/
function add_posts_columns_custom_row($column_name, $post_id) {

  if ($column_name == 'thumbnail') {
    $thumb = get_the_post_thumbnail($post_id, array(80,80), 'thumbnail'); // 画像のサイズを指定しておく
    echo ( $thumb ) ? $thumb : '-';  // 値がないときの処理をしておく
  }

  if ($column_name == 'postid') {
    echo $post_id;
  }

}

/**
* カスタム投稿タイプ《column》のみ
*/
function add_posts_columns_custom_row_column($column_name, $post_id) {

  if ($column_name == 'writer') {
    $writer = get_the_term_list($post_id, 'writer', '', ', ');
    echo ( $writer ) ? $writer : '-';
  }

  if ($column_name == 'column-rank') {
    $rank = get_post_meta($post_id, 'column-rank', true);
    echo ( $rank ) ? $rank : '-';
  }

  if ($column_name == 'column-pickup') {
    $pickup = get_post_meta($post_id, 'column-pickup', true);
    echo ( $pickup ) ? $pickup : '-';
  }

  if ($column_name == 'column-score-num') {
    $score_num = get_post_meta($post_id, 'column-score-num', true);
    $score_total = get_post_meta($post_id, 'column-score-total', true);
    $score = $score_total / $score_num;
    echo ( $score ) ? $score : '-';
  }

}

add_filter( 'manage_posts_columns', 'add_posts_columns_custom' );
add_action( 'manage_posts_custom_column', 'add_posts_columns_custom_row', 10, 2 );
add_filter( 'manage_column_posts_columns', 'add_posts_columns_custom_column' );
add_action( 'manage_column_posts_custom_column', 'add_posts_columns_custom_row_column', 10, 2 );


/**
* 投稿一覧ソート機能
*
*/
function custom_sortable_columns($sort_column) {

  $sort_column['column-rank'] = 'column-rank';
  $sort_column['writer'] = 'writer';
  $sort_column['postid'] = 'ID';
  return $sort_column;

}

function custom_orderby_columns( $vars ) {

  if (isset($vars['orderby']) && 'column-rank' == $vars['orderby']) {
    $vars = array_merge($vars, array(
      'orderby' => 'meta_value_num',
      'meta_query' => array(
        array(
          'key' => 'column-rank',
          'value' => array(0), // 値が0のものを除く
          'compare' => '>',
        ),
      ),
    ));
  }
  if (isset($vars['orderby']) && 'writer' == $vars['orderby']) {
    $vars = array_merge($vars, array(
      'taxonomy' => 'writter',
      'orderby' => 'name',
    ));
  }
  if (isset($vars['orderby']) && 'postid' == $vars['orderby']) {
    $vars = array_merge($vars, array(
      'orderby' => 'ID',
    ));
  }
  return $vars;

}
add_filter( 'manage_edit-column_sortable_columns', 'custom_sortable_columns' ); // manage_edit-[post_type]_sortable_columns
add_filter( 'request', 'custom_orderby_columns' );

10
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?