管理画面での「絞り込み検索」

Simplicityの特徴 フォーラム Simplicity2に関する話題何でも 管理画面での「絞り込み検索」

  • このトピックには11件の返信、2人の参加者があり、最後にtechnoteにより8年、 4ヶ月前に更新されました。
11件の返信スレッドを表示中
  • 投稿者
    投稿
    • #46893
      盛土
      ゲスト

      テーマと関係なくスミマセン

      WP管理画面の投稿一覧で記事を探すとき
      日付とカテゴリーで絞り込み検索できるのですが
      タグで絞り込み検索できないかと

      色々と調べているのですが
      サイト上で記事を検索するものばかりで・・

    • #46899
      アバター画像わいひら
      キーマスター

      ここら辺あたりの方法でできるのではないかと思います。
      WordPressの投稿一覧でタグのフィルタリングを追加する | Simple Colors
      ただ、実際に試したわけではないので、保証はないです。
      うまくいかなかった場合に、複雑なカスタマイズはサポート外となるので、何とか自前で行っていただけると助かります。
      サポート対象外のもの

    • #46901
      盛土
      ゲスト

      できました!!

      教えて頂きましたLINK先のコードを
      子テーマfunctions.phpにそのままコピペしました

      カテゴリーみたいにプルダウンでないのが残念ですが
      カテゴリーと組み合わせて検索でき
      編集作業が捗り満足してます

      アドバイス頂きまして
      ありがとうございました
      m(_ _)m

    • #46923
      technote
      ゲスト

      以下のコードでプルダウンを追加することは可能です。

      add_action('restrict_manage_posts', create_function('', <<< EOT
      	global \$post_type, \$tag;
      	if ( is_object_in_taxonomy( \$post_type, 'post_tag' ) ) {
      		\$dropdown_options = array(
      			'show_option_all' => get_taxonomy( 'post_tag' )->labels->all_items,
      			'hide_empty' => 0,
      			'hierarchical' => 1,
      			'show_count' => 0,
      			'orderby' => 'name',
      			'selected' => \$tag,
      			'name' => 'tag',
      			'taxonomy' => 'post_tag',
      			'value_field' => 'slug'
      		);
      		wp_dropdown_categories( \$dropdown_options );
      	}
      EOT
      ));
    • #46927
      翔平
      ゲスト

      technote様

      ありがとうございます
      見事にプルダウンが表示されました!

      でも
      「すべてのタグ」で検索するとなにもヒットしません
      任意のタグで絞り込むとヒットします

      つまり
      カテゴリーだけでの検索が出来ないようです

      以上
      ご報告まで

    • #46929
      アバター画像わいひら
      キーマスター

      紹介したカスタマイズを自分でも試したところ、よさげで、次のバージョンでこの機能を実装しようと思っていたので、それ用のコードも載せておきます。
      次のバージョンを採用する際には、今回追加したコードは削除したほうがいいかもしれません。

      //投稿一覧リストの上にタグフィルターを追加する
      if ( !function_exists( 'add_tag_restrict_filter_on_post_list' ) ):
      function add_tag_restrict_filter_on_post_list() {
          global $post_type;
          if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
            $tag_name = $_GET && $_GET['tag_name'] ? $_GET['tag_name'] : '';
      ?>
        <?php $tags = get_tags(); if ( $tags ) : ?>
        <select name="tag_name">
          <option value=""<?php echo $tag_name ? '' : ' selected="selected"'; ?>>タグを選択</option>
          <?php foreach ( $tags as $tag ): ?>
          <option value="<?php echo $tag->name; ?>"<?php echo $tag_name == $tag->name ? ' selected="selected"' : ''; ?>><?php echo esc_html( $tag->name ); ?> (<?php echo $tag->count; ?>)</option>
          <?php endforeach; ?>
        </select>
        <?php endif; ?>
        <!-- <input name="tag_name" size="25" value="<?php echo esc_html( $_GET ? $_GET['tag_name'] : '' ); ?>" class="postform" /> -->
      <?php
          }
      }
      endif;
      add_action( 'restrict_manage_posts', 'add_tag_restrict_filter_on_post_list' );
      
      //タグネームをタグスラッグに変換する
      if ( !function_exists( 'convert_tag_name_to_tag_slug' ) ):
      function convert_tag_name_to_tag_slug() {
        if ( ! isset( $_GET['post_type'] ) ) {
          $post_type = 'post';
        } elseif ( in_array( $_GET['post_type'], get_post_types( array( 'show_ui' => true ) ) ) ) {
          $post_type = $_GET['post_type'];
        } else {
          wp_die( __('Invalid post type') );
        }
      
        if ( ! is_object_in_taxonomy( $post_type, 'post_tag' ) || ! isset( $_GET['tag_name'] ) ) {
          return;
        }
        if ( is_array( $_GET['tag_name'] ) ) {
            $_GET['tag_name'] = implode( ',', $_GET['tag_name'] );
        }
        $tag_name = explode( ',', $_GET['tag_name'] );
        $tag_name = array_map( 'trim', $tag_name );
        if ( $tag_name ) {
          $tags = get_tags( 'hide_empty=0&orderby=slug' );
          $tags_arr = array();
          if ( $tags ) {
            foreach ( $tags as $tag ) {
              $tags_arr[$tag->name] = $tag->slug;
            }
          } else {
            unset( $_GET['tag_name'] );
            return;
          }
          $searh_tags = array();
          $matched_tags = array();
          foreach ( $tag_name as $t_name ) {
            if ( isset( $tags_arr[$t_name] ) ) {
              $searh_tags[] = $tags_arr[$t_name];
              $matched_tags[] = $t_name;
            }
          }
          $searh_tags = implode( ' ', $searh_tags );
          // OR 検索にしたい場合は、カンマ繋ぎにする
          //      $searh_tags = implode( ',', $searh_tags );
          if ( $searh_tags ) {
            $_GET['tag'] = $searh_tags;
            $_GET['tag_name'] = implode( ',', $matched_tags );
          } else {
            unset( $_GET['tag_name'] );
          }
        }
      }
      endif;
      add_action( 'load-edit.php', 'convert_tag_name_to_tag_slug' );
    • #46931
      technote
      ゲスト

      タグを選択していない場合の動作を忘れていました。

      add_action('load-edit.php', create_function('', <<< EOT
      	if (isset(\$_GET['tag']) && '0' === \$_GET['tag']) {
      		unset (\$_GET['tag']);
      	}
      EOT
      ));

      ついでに作成者で絞り込むプルダウンも追加してみました。

      add_action('restrict_manage_posts', create_function('', <<< EOT
      	global \$post_type, \$tag;
      	if ( is_object_in_taxonomy( \$post_type, 'post_tag' ) ) {
      		\$dropdown_options = array(
      			'show_option_all' => get_taxonomy( 'post_tag' )->labels->all_items,
      			'hide_empty' => 0,
      			'hierarchical' => 1,
      			'show_count' => 0,
      			'orderby' => 'name',
      			'selected' => \$tag,
      			'name' => 'tag',
      			'taxonomy' => 'post_tag',
      			'value_field' => 'slug'
      		);
      		wp_dropdown_categories( \$dropdown_options );
      	}
      
      	wp_dropdown_users(
      		array(
      			'show_option_all' => 'すべてのユーザー',
      			'name' => 'author'
      		)
      	);
      EOT
      ));
    • #46932
      technote
      ゲスト

      わいひら様
      プルダウン作成時にvalueにslugを使用すれば、「タグネームをタグスラッグに変換する」処理は必要ありません。

      //投稿一覧リストの上にタグフィルターを追加する
      if ( !function_exists( 'add_tag_restrict_filter_on_post_list' ) ):
      	function add_tag_restrict_filter_on_post_list() {
      		global $post_type;
      		if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
      			$_tag = $_GET && $_GET['tag'] ? $_GET['tag'] : '';
      			?>
      			<?php $tags = get_tags(); if ( $tags ) : ?>
      				<select name="tag">
      					<option value=""<?php echo $_tag ? '' : ' selected="selected"'; ?>>タグを選択</option>
      					<?php foreach ( $tags as $tag ): ?>
      						<option value="<?php echo $tag->slug; ?>"<?php echo $_tag == $tag->slug ? ' selected="selected"' : ''; ?>><?php echo esc_html( $tag->name ); ?> (<?php echo $tag->count; ?>)</option>
      					<?php endforeach; ?>
      				</select>
      			<?php endif; ?>
      			<!-- <input name="tag_name" size="25" value="<?php echo esc_html( $_GET ? $_GET['tag_name'] : '' ); ?>" class="postform" /> -->
      			<?php
      		}
      	}
      endif;
      add_action( 'restrict_manage_posts', 'add_tag_restrict_filter_on_post_list' );
    • #46933
      アバター画像わいひら
      キーマスター

      確かに、僕の書いたのはコピペをただ動くものにしただけなので、なんか二度手間みたいなことになってますね^^;
      というか、technoteさんの書かれている方法のほうが、スマートでコードも見やすいので次のバージョンでは、こちらを使わせていただこうと思います。

      //投稿一覧リストの上にタグフィルターと管理者フィルターを追加する
      if ( !function_exists( 'custmuize_restrict_manage_posts' ) ):
      function custmuize_restrict_manage_posts(){
        global $post_type, $tag;
        if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
          $dropdown_options = array(
            'show_option_all' => get_taxonomy( 'post_tag' )->labels->all_items,
            'hide_empty' => 0,
            'hierarchical' => 1,
            'show_count' => 0,
            'orderby' => 'name',
            'selected' => $tag,
            'name' => 'tag',
            'taxonomy' => 'post_tag',
            'value_field' => 'slug'
          );
          wp_dropdown_categories( $dropdown_options );
        }
      
        wp_dropdown_users(
          array(
            'show_option_all' => 'すべてのユーザー',
            'name' => 'author'
          )
        );
      }
      endif;
      add_action('restrict_manage_posts', 'custmuize_restrict_manage_posts');

      使わせていただきます。ありがとうございます!

    • #46934
      アバター画像わいひら
      キーマスター

      あ、「全てのタグ」選択時の処理も書く必要があるのか。

      //投稿一覧リストの上にタグフィルターと管理者フィルターを追加する
      if ( !function_exists( 'custmuize_restrict_manage_posts' ) ):
      function custmuize_restrict_manage_posts(){
        global $post_type, $tag;
        if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
          $dropdown_options = array(
            'show_option_all' => get_taxonomy( 'post_tag' )->labels->all_items,
            'hide_empty' => 0,
            'hierarchical' => 1,
            'show_count' => 0,
            'orderby' => 'name',
            'selected' => $tag,
            'name' => 'tag',
            'taxonomy' => 'post_tag',
            'value_field' => 'slug'
          );
          wp_dropdown_categories( $dropdown_options );
        }
      
        wp_dropdown_users(
          array(
            'show_option_all' => 'すべてのユーザー',
            'name' => 'author'
          )
        );
      }
      endif;
      add_action('restrict_manage_posts', 'custmuize_restrict_manage_posts');
      
      //投稿一覧で「全てのタグ」選択時は$_GET['tag']をセットしない
      if ( !function_exists( 'custmuize_load_edit_php' ) ):
      function custmuize_load_edit_php(){
        if (isset($_GET['tag']) && '0' === $_GET['tag']) {
          unset ($_GET['tag']);
        }
      }
      endif;
      add_action('load-edit.php', 'custmuize_load_edit_php');
    • #46935
      盛土
      ゲスト

      プルダウン実装できました!
      (バージョンアップ時には削除します)

      絞り込み検索上の「投稿を検索」でも
      タグを検索できると
      良いんですけどね

      ありがとうございました
      m(_ _)m

    • #46936
      technote
      ゲスト

      わいひら様
      対応ありがとうございます。

11件の返信スレッドを表示中
  • トピック「管理画面での「絞り込み検索」」には新しい返信をつけることはできません。
スポンサーリンク
アドセンス(大)
アドセンス(大)