PHP
1function.php 2 3add_action( 'restrict_manage_posts', 'add_term_dropdown', 10, 2 ); 4function add_term_dropdown( $post_type ) { 5 if ( 'products' == $post_type ) { 6 7 $term_slug = get_query_var('products_cat'); 8 $taxonomy_var = get_taxonomy('products_cat'); 9 10 wp_dropdown_categories( array( 11 'show_option_all' => $taxonomy_var->label, 12 'selected' => $term_slug, 13 'name' => 'products_cat', 14 'taxonomy' => 'products_cat', 15 'value_field' => 'slug', 16 )); 17 } 18} 19
こちらのコードの場合だと、選択状態がキッチリ保存されるのですが、
この場合、複数タームでの絞り込みが出来ません。
下記がforeachで、セレクトボックスを複数表示してみたコードです。
PHP
1add_action( 'restrict_manage_posts', 'add_term_dropdown', 10, 2 ); 2function add_term_dropdown( $post_type ) { 3 4 $products_cats = ['products_cat_1', 'products_cat_2', 'products_cat_3']; 5 6 if ('products' == $post_type) { 7 foreach ($products_cats as $products_cat) { 8 9 $term_slug = get_query_var($products_cat); 10 $taxonomy_var = get_taxonomy($products_cat); 11 12 wp_dropdown_categories(array( 13 'show_option_all' => $taxonomy_var->label, 14 'selected' => $term_slug, 15 'name' => $products_cat, 16 'taxonomy' => $products_cat, 17 'value_field' => 'slug', 18 )); 19 } 20 } 21} 22
このコードで複数セレクトボックスは表示されるのですが、
絞り込み検索をしてみると、**「無効な投稿形式」**と表示されてしまい
絞り込み検索ができません。
関数定義の部分のfunction add_term_dropdown( $post_type ) {
()内の$post_type
を消して
関数内にglobal $post_type;
を追記すると問題なく検索できますが、今度は選択状態が保存されません。
この場合どのように記載するのが正しいのでしょうか。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/11 11:05
退会済みユーザー
2020/01/11 23:30
2020/01/14 09:41