4. 1 カテゴリ別投稿一覧(簡易版)

 カテゴリ別投稿一覧の簡易版です。投稿一覧でもカテゴリを指定すれば表示できますが、一般ユーザーが使いやすいものではありません。
 機能は次のものに絞っています。
  ・編集
  ・ゴミ箱
  ・表示
  ・復元
  ・完全に削除
 これらメニューは常に表示されます。
 他者が編集中の投稿は編集できません。
 子カテゴリは除外して表示します。
 作成者は、当然、氏名表示。
 ゴミ箱・復元・削除でリダイレクトはしません。このプログラムでクローズしています。

 このコードを起動させるのには、アドミニメニューにメニューを登録する必要があります。アドミニメニューの設定は別途説明します。
 下記関数は、“共通関数”に記載します。
   ・hk_func_orderby_select
   ・hk_func_order_select
   ・hk_func_author_select
   ・hk_func_status_select
 フック'admin_action_'は、admin.php の最後にあります。admin.php の処理が終了したところで、“分岐”しています。

コード

// ================================
// カテゴリ別投稿一覧(簡易版)
// ================================
function hk_edit_list() {
    require_once( WP_PLUGIN_DIR.'/hk-easy/hk-edit-list.php' );
    exit;
}
add_action( 'admin_action_hk_edit_list', 'hk_edit_list' );

// リンク カテゴリ別投稿一覧
function hk_easy_get_link_cat_post_list( $cat_id ) {
    return 'edit.php?action=hk_edit_list&post_type=post&post_status=any&cat='.$cat_id.'&hk_light_type=cat_post_list';
}
// リンク カテゴリ別投稿 ゴミ箱
function hk_easy_get_link_cat_post_trash_list( $cat_id ) {
    return 'edit.php?action=hk_edit_list&post_type=post&post_status=trash&cat='.$cat_id.'&hk_light_type=cat_post_list';
}
// リンク カテゴリ別投稿 新規作成
function hk_easy_get_link_cat_post_new( $cat_id ) {
    return 'admin.php?action=hk_post_new&hk_cat_id='.$cat_id.'&hk_light_type=cat_post_list';
}
// リンク 固定ページ編集
function hk_easy_get_link_page( $post_id ) {
    return 'post.php?post='.$post_id.'&action=edit&hk_light_type=edit_page';
}
// リンク(URL) 投稿編集
function hk_easy_get_edit_post_url( $post_id, $cat_id ) {
    return get_edit_post_link( $post_id, '' ).'&hk_cat_id='.$cat_id.'&hk_light_type=cat_post_list';
}

hk-edit-list.php
<?php
// ================================
// カテゴリ別投稿一覧(簡易版)
// ================================

// --------------------------------
// スタイル
// --------------------------------
function hk_edit_list_admin_head() {
?>
<style type="text/css">
    .hk_list_table {
        border-collapse: collapse;
        border-spacing: 0;
        border-style: none;
        color: #000000;
        width: 100%;
        margin: 0;
    }
    .hk_list_table thead,
    .hk_list_table tbody {
        border:1px #a9a9a9 solid;
    }
    .hk_list_table th,
    .hk_list_table td {
        padding: 6px 12px;
        padding: 0.428571428rem 0.857142857rem;
        font-weight: normal;
        text-align: left;
        vertical-align: middle;
    }
    .hk_list_table_cb {
        width: 24px;
        width: 1.714285714rem;
    }
</style>
<?php
    return;
}
add_action( 'admin_head', 'hk_edit_list_admin_head' );

// ================================
// メイン
// ================================
global $title, $parent_file;
global $wp_query;
global $post;
// hk_light_type
if ( ! isset( $_REQUEST['hk_light_type'] ) )    wp_die('ERROR:hk_light_type');
$light_type = $_REQUEST['hk_light_type'];
if ( $light_type != 'cat_post_list' )            wp_die('ERROR:hk_light_type');
// post_type
if ( ! isset( $_REQUEST['post_type'] ) )        wp_die('ERROR:post_type');
$post_type = $_REQUEST['post_type'];
if ( $post_type != 'post' )                        wp_die('ERROR:post_type');
// post_status
if ( ! isset( $_REQUEST['post_status'] ) )        wp_die('ERROR:post_status');
$post_status = $_REQUEST['post_status'];
// カテゴリーID
if ( ! isset( $_REQUEST['cat'] ) )                wp_die('ERROR:cat');
$cat_id = $_REQUEST['cat'];
// カテゴリー名称
$cat_name = get_cat_name( $cat_id );
// 投稿取得パラメータ
if ( isset( $_REQUEST['orderby'] ) )
        $orderby = $_REQUEST['orderby'];
else    $orderby = 'modified';
if ( isset( $_REQUEST['order'] ) )
        $order = $_REQUEST['order'];
else    $order = 'DESC';
if ( isset( $_REQUEST['author'] ) )
        $author = $_REQUEST['author'];
else    $author = '';
if ( isset( $_REQUEST['paged'] ) )
        $paged = absint( $_REQUEST['paged'] );
else    $paged = 1;
// ゴミ箱への移動、復元、削除
if ( isset( $_REQUEST['doaction'] ) )
        $do_action = $_REQUEST['doaction'];
else    $do_action = '';
$message = '';
if ( $do_action ) {
    // POST_ID
    if ( isset( $_REQUEST['post'] ) )
            $post_id = (int) $_REQUEST['post'];
    else    $post_id = 0;
    if ( $post_id )
        $post = get_post( $post_id );
    else    wp_die('ERROR:post');
    switch( $do_action ) {
        case 'trash':
            check_admin_referer( 'trash-post_'.$post_id );
            if ( ! $post )
                wp_die('ゴミ箱に移動しようとしている項目が存在しません。');
            if ( ! current_user_can( 'delete_post', $post_id ) )
                wp_die('この項目をゴミ箱へ移動する権限がありません。');
            if ( $user_id = wp_check_post_lock( $post_id ) )
                wp_die('この項目をゴミ箱へ移動することはできません。現在編集中です。');
            if ( ! wp_trash_post( $post_id ) )
                wp_die('ゴミ箱への移動中にエラーが発生しました。');
            $message = '投稿をゴミ箱へ移動しました。';
            break;
        case 'untrash':
            check_admin_referer('untrash-post_'.$post_id);
            if ( ! $post )
                wp_die('ゴミ箱から復元しようとしている項目が存在しません。');
            if ( ! current_user_can( 'delete_post', $post_id ) )
                wp_die('この項目をゴミ箱から復元する権限がありません。');
            if ( ! wp_untrash_post( $post_id ) )
                wp_die('ゴミ箱からの復元中にエラーが発生しました。');
            $message = '投稿をゴミ箱から復元しました。';
            break;
        case 'delete':
            check_admin_referer('delete-post_'.$post_id);
            if ( ! $post )
                wp_die('この項目はすでに削除されています。');
            if ( ! current_user_can( 'delete_post', $post_id ) )
                wp_die('この項目を削除する権限がありません。');
            if ( ! wp_delete_post( $post_id, true ) )
                wp_die('削除中にエラーが発生しました。');
            $message = '投稿を永久に削除しました。';
            break;
    } // end switch
}
// ヘッダー
if ( $post_status != 'trash' )
        $page_title = $cat_name.' 投稿一覧(簡易版)';
else    $page_title = $cat_name.' ゴミ箱(簡易版)';
$title = $page_title;
$parent_file = hk_easy_get_link_cat_post_list( $cat_id );
// admin-header
require_once( ABSPATH . 'wp-admin/admin-header.php' );
// ページタイトル
echo "\n".'<div class="wrap">'."\n";
echo '  <h1>'.esc_html( $page_title ).'</h1>'."\n";
// メッセージ
if ( $message )
    echo '  <div id="message" class="updated notice is-dismissible"><p>'.$message."</p></div>\n";
// 投稿一覧へのリンク
$link_list     = hk_easy_get_link_cat_post_list( $cat_id );
// ゴミ箱へのリンク
$link_trash    = hk_easy_get_link_cat_post_trash_list( $cat_id );
// 新規作成のリンク
$link_post_new = hk_easy_get_link_cat_post_new( $cat_id );
// 現在ページへのリンク
if ( $post_status == 'trash' )
    $edit_url = $link_trash;
else {
    $edit_url = $link_list;
    $edit_url = add_query_arg( 'post_status', $post_status, $edit_url );
}
$edit_url .= '&orderby='.$orderby.'&order='.$order.'&author='.$author.'&paged='.$paged;
// ローカルメニュー
// 一覧 ローカルメニュー
if ( $post_status != 'trash' )
    $local_menu = '<a href="'.$link_post_new.'">新規作成</a> <a href="'.$link_trash.'">ゴミ箱</a>';
// ゴミ箱 ローカルメニュー
else
    $local_menu = '<a href="'.$link_list.'">投稿一覧</a>';
echo '  <p>'.$local_menu."</p>\n";
// 投稿取得 (子カテゴリ除外)
$args = array(
    'post_type'      => 'post',
    'post_status'    => $post_status,
    'category__in'   => $cat_id,
    'orderby'        => $orderby,
    'order'          => $order,
    'author'         => $author,
    'paged'          => $paged,
    'posts_per_page' => 20,
);
query_posts( $args );
// FORM
echo '<form method="get">'."\n";
echo '  <input type="hidden" name="action"        value="hk_edit_list" />'."\n";
echo '  <input type="hidden" name="post_type"     value="post" />'."\n";
echo '  <input type="hidden" name="hk_light_type" value="cat_post_list" />'."\n";
echo '  <input type="hidden" name="cat"           value="'.$cat_id.'" />'."\n";
// 一覧表上部メニュー 表示順序変更 & ページネーション
echo '  <div class="tablenav top hk_clear">'."\n";
// 表示順序変更
echo '    <div class="alignleft">'."\n";
// 順序項目選択
hk_func_orderby_select( $orderby );
// 順序選択
hk_func_order_select( $order );
// 作成者選択
hk_func_author_select( $author );
// 状態選択
if ( $post_status != 'trash' )
    hk_func_status_select( $post_status );
// 送信ボタン
echo '      <input type="submit" value="表示変更" />'."\n";
echo "    </div>\n";
// ページネーション
$total_items = $wp_query->found_posts;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages ) {
    if ( $total_pages < 2 )    $page_class = ' one-page';
    else                    $page_class = '';
}
else                        $page_class = ' no-pages';
echo '    <div class="tablenav-pages'.$page_class.'">'."\n";
echo '      <span class="displaying-num">'.$total_items.'項目</span>'."\n";
// 現在ページ
$current = $paged;
$current_url = admin_url( $edit_url );
echo '      <span class="pagination-links">'."\n";
// 最初のページへ
$url   = $current_url;
$url   = remove_query_arg( 'paged', $url );
$url_1 = esc_url( $url );
echo '        <a class="first-page" title="最初のページへ" href="'.$url_1.'">&laquo;</a>'."\n";
// 前のページへ
$page_no = max( 1, $current-1 );
$url   = $current_url;
$url   = add_query_arg( 'paged', $page_no, $url );
$url_2 = esc_url( $url );
echo '        <a class="prev-page" title="前のページへ" href="'.$url_2.'">&lsaquo;</a>'."\n";
echo '        <span class="paging-input">'."\n";
// 現在のページ
echo '          <label for="current-page-selector" class="screen-reader-text">ページを選択</label>'."\n";
$size = strlen( $total_pages );
echo '          <input class="current-page" id="current-page-selector" title="現在のページ" type="text" name="paged" value="'.$current.'" size="'.$size.'" /> / <span class="total-pages">'.$total_pages.'</span>'."\n";
echo '        </span>'."\n";
// 次のページへ
$page_no = min( $total_pages, $current+1 );
$url   = $current_url;
$url   = add_query_arg( 'paged', $page_no, $url );
$url_3 = esc_url( $url );
echo '        <a class="next-page" title="次のページへ" href="'.$url_3.'">&rsaquo;</a>'."\n";
// 最後のページへ
$url   = $current_url;
$url   = add_query_arg( 'paged', $total_pages, $url );
$url_4 = esc_url( $url );
echo '        <a class="last-page" title="最後のページへ" href="'.$url_4.'">&raquo;</a>'."\n";
echo '      </span>'."\n";
echo '    </div>'."\n";
// ページネーション終わり
echo '  </div>'."\n";
// form 終わり
echo "</form>\n";
// 一覧表
echo '  <table class="hk_list_table">'."\n";
// 項目名
echo "    <thead>\n";
echo "      <tr>\n";
echo '        <th scope="col" class="hk_list_table_cb"></th>'."\n";
echo '        <th scope="col">タイトル</th>'."\n";
echo '        <th scope="col">状態</th>'."\n";
echo '        <th scope="col">作成者</th>'."\n";
echo '        <th scope="col">作成日</th>'."\n";
echo '        <th scope="col">更新日</th>'."\n";
echo "      </tr>\n";
echo "    </thead>\n";
// データ
$current_user_id = get_current_user_id();
if ( have_posts() ) {
    echo "    <tbody>\n";
    while ( have_posts() ) {
        the_post();
        $post_id = $post->ID;
        $can_edit_post = current_user_can( 'edit_post', $post_id );
        $lock_holder = wp_check_post_lock( $post_id );
        // $post_status 投稿データに変更
        $post_status = $post->post_status;
        // 作成者
        $author_id   = $post->post_author;
        // 行表示開始
        echo '      <tr>'."\n";
        // チェックボックス
        echo '        <td>';
        if ( $can_edit_post ) {
            if ( ! $lock_holder )    echo '<span class="dashicons dashicons-edit"></span>';
            else                    echo '<span class="dashicons dashicons-lock"></span>';
        }
        echo '</td>'."\n";
        // タイトル
        echo '        <td>'."\n";
        $edit_link = hk_easy_get_edit_post_url( $post_id, $cat_id );
        if ( isset( $post->post_title ) ) {
            $post_title = $post->post_title ;
            if ( $post_title == '' )        $post_title = '(タイトルなし)';
        }
        else $post_title = '(タイトルなし)';
        $post_title = htmlspecialchars( $post_title );
        echo '<strong>';
        if ( $can_edit_post && $post_status != 'trash' ) {
            if ( ! $lock_holder )
                    echo '<a href="'.$edit_link.'" title="編集 '.$post_title.'">'.$post_title.'</a>';
            else    echo $post_title;
        }
        else    echo $post_title;
        echo '</strong>'."\n";
        if ( $lock_holder )
            $lock_holder = get_userdata( $lock_holder );
        if ( $can_edit_post && $post_status != 'trash' ) {
            if ( $lock_holder ) {
                $lock_holder_name = $lock_holder->last_name.' '.$lock_holder->first_name;
                echo '<br /> 現在 '.$lock_holder_name.' さんが編集中です'."\n";
            }
        }
        $actions = array();
        if ( ! $lock_holder ) {
            // 他者が編集中でない場合
            if ( $can_edit_post && 'trash' != $post_status )
                $actions['edit'] = '<a href="'.$edit_link.'" title="このタイトルを編集">編集</a>';
            if ( current_user_can( 'delete_post', $post_id ) ) {
                if ( $post_status == 'trash' ) {
                    $untrash_url = $edit_url.'&post='.$post_id.'&doaction=untrash';
                    $untrash_url = admin_url( $untrash_url );
                    $untrash_url = wp_nonce_url( $untrash_url, 'untrash-post_'.$post_id );
                    $actions['untrash'] = '<a title="この項目をゴミ箱から復元する" href="'.$untrash_url.'">復元</a>';
                    $delete_url = $edit_url.'&post='.$post_id.'&doaction=delete';
                    $delete_url = admin_url( $delete_url );
                    $delete_url = wp_nonce_url( $delete_url, 'delete-post_'.$post_id );
                    $actions['delete'] = '<a title="この項目を完全に削除する" href="'.$delete_url.'">完全に削除する</a>';
                }
                else {
                    $trash_url = $edit_url.'&post='.$post_id.'&doaction=trash';
                    $trash_url = admin_url( $trash_url );
                    $trash_url = wp_nonce_url( $trash_url, 'trash-post_'.$post_id );
                    $actions['trash'] = '<a title="この項目をゴミ箱へ移動する" href="'.$trash_url.'">ゴミ箱</a>';
                }
            }
            if ( $post_status != 'trash' ) {
                if (( $author_id == $current_user_id )
                 ||    ( $post_status != 'draft' ))
                    $actions['view'] = '<a title="この項目を表示する" href="'.get_permalink().'" target="_blank">表示</a>';
            }
        }
        if ( ! empty( $actions ) )
            echo '<br /> '.implode( " | \n", $actions )."\n";
        echo '        </td>'."\n";
        // 状態
        if      ( $post_status == 'publish' )    $post_status_jp = '公開中';
        else if ( $post_status == 'pending' )    $post_status_jp = '承認待';
        else if ( $post_status == 'draft' )        $post_status_jp = '下書き';
        else if ( $post_status == 'future' )    $post_status_jp = '公開待';
        else if ( $post_status == 'private' )    $post_status_jp = '非公開';
        else if ( $post_status == 'trash' )        $post_status_jp = 'ゴミ箱';
        else                                    $post_status_jp = '   ';
        echo '        <td>'.$post_status_jp."</td>\n";
        // 作成者
        $section = get_user_meta( $author_id, 'hk_section', true );
        $section = htmlspecialchars( $section );
        $author_obj = get_userdata( $author_id );
        if ( $author_obj )
                $author_name = $author_obj->last_name.' '.$author_obj->first_name;
        else    $author_name = '------';
        if ( $section != '' )
            $author_name = $section.'<br />'.$author_name;
        echo '        <td>'.$author_name."</td>\n";
        // 作成日
        $date  = get_the_date('Y/m/d');
        $dates = explode( '/', $date );
        $date  = $dates[0].'年'.$dates[1].'月'.$dates[2].'日';
        echo '        <td>'.$date."</td>\n";
        // 更新日
        $modified_date  = get_the_modified_date('Y/m/d');
        $modified_dates = explode( '/', $modified_date );
        $modified_date  = $modified_dates[0].'年'.$modified_dates[1].'月'.$modified_dates[2].'日';
        echo '        <td>'.$modified_date."</td>\n";
        echo "      </tr>\n";
    }
    echo "    </tbody>\n";
    echo "  </table>\n";
    // 一覧表下部
    echo '  <div class="tablenav bottom hk_clear">'."\n";
    // ページネーション
    echo '    <div class="tablenav-pages'.$page_class.'">'."\n";
    echo '      <span class="displaying-num">'.$total_items.'項目</span>'."\n";
    echo '      <span class="pagination-links">'."\n";
    // 最初のページへ
    echo '        <a class="first-page" title="最初のページへ" href="'.$url_1.'">&laquo;</a>'."\n";
    // 前のページへ
    echo '        <a class="prev-page" title="前のページへ" href="'.$url_2.'">&lsaquo;</a>'."\n";
    // 現在のページ
    echo '        <span class="screen-reader-text">現在のページ</span>'."\n";
    echo '        <span class="paging-input">'.$current.' / <span class="total-pages">'.$total_pages.'</span></span>'."\n";
    // 次のページへ
    echo '        <a class="next-page" title="次のページへ" href="'.$url_3.'">&rsaquo;</a>'."\n";
    // 最後のページへ
    echo '        <a class="last-page" title="最後のページへ" href="'.$url_4.'">&raquo;</a>'."\n";
    echo '      </span>'."\n";
    echo '    </div>'."\n";
    // ページネーション終わり
    echo '  </div>'."\n";
}
else {
    echo "    <tbody>\n";
    echo '      <tr><td colspan="6">';
    if ( $post_status != 'trash' )
            echo '投稿はありません。';
    else    echo 'ゴミ箱は空です。';
    echo "</td></tr>\n";
    echo "    </tbody>\n";
    echo "  </table>\n";
}
echo "</div>\n";
// フッター
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit;
?>

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

関連

更新日:2016/03/24
掲載日:2016/01/14