2017-12-18 6 views
0

worpressのデータベーステーブルクエリの値と同じ数のドロップダウンを作成する必要があります。私はforeachドロップダウンリストからUsermetaテーブルに投稿データを取得できません。これはコードです:SELECTのドロップダウンから可変値のPOSTデータを取得

$custom_posts_type = $wpdb->get_results("SELECT label,id FROM $table_name"); 
     foreach ($custom_posts_type as $custom_post_type) { 
      $custom_post_type_name = $custom_post_type->label; 
      $custom_post_type_id = $custom_post_type->id; 
      ?> 
      <p> 
      <label for="dropdown">Permisos de usuario para <?php echo $custom_post_type_name;?>: </label> 
      <?php            
      $user_custom_post = 'user_custom_' . $custom_post_type_name; 
      $selected = get_the_author_meta($user_custom_post, $user->ID); 
      $post_type_object = get_post_type_object($custom_post_type_name);       
      $label = $post_type_object->label; 
      $posts = get_posts(array('post_type'=> $custom_post_type_name, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));       
      echo '<select name="' . $user_custom_post . '" id="' . $user_custom_post . '">';            
      ?> 
      <option value="all" <?php selected($user_custom_post[0], "all"); ?>>All <?php echo $label;?></option> 
      <?php 
      foreach ($posts as $post) { 
       echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>'; 
      }?>    
      <?php 
      echo '</select>'; 
      ?>       
      </p> 
      <?php 
      update_user_meta($user->ID, $user_custom_post, $_POST[$user_custom_post]);       
} //endforeach 

ご協力いただきありがとうございます。 Thks

+0

...

add_action('show_user_profile', 'pglr_user_pages'); add_action('edit_user_profile', 'pglr_user_pages'); add_action('user_register', 'pglr_user_pages'); function pglr_user_pages($user) { $post_type = "page"; ?> <p> <label for="dropdown">Asignar <?php echo $post_type;?>: </label> <?php **global $user_custom_post;** **$user_custom_post = 'user_custom_' . $post_type;** $selected = get_the_author_meta(**$user_custom_post**, $user->ID); $post_type_object = get_post_type_object($post_type); $label = $post_type_object->label; $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1)); echo '<select name="user_custom_page_value" id="user_custom_page_value">'; ?> <option value="all" <?php selected('', "all"); ?>>All <?php echo $label;?></option> <?php foreach ($posts as $post) { echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>'; }?> <?php echo '</select>'; ?> </p> <?php } add_action('personal_options_update', 'save_pglr_user_pages'); add_action('edit_user_profile_update', 'save_pglr_user_pages'); function save_pglr_user_pages($user_id) { if (!current_user_can('edit_user', $user_id)) return false; //save dropdown update_usermeta($user_id, **$user_custom_post,** $_POST['user_custom_page_value']); } 

問題は変数を使用している可能性がある。しかし、このコードは動作しません

add_action('show_user_profile', 'pglr_user_pages'); add_action('edit_user_profile', 'pglr_user_pages'); add_action('user_register', 'pglr_user_pages'); function pglr_user_pages($user) { $post_type = "page"; ?> <p> <label for="dropdown">Asignar <?php echo $post_type;?>: </label> <?php $selected = get_the_author_meta('user_custom_page', $user->ID); $post_type_object = get_post_type_object($post_type); $label = $post_type_object->label; $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1)); echo '<select name="user_custom_page_value" id="user_custom_page_value">'; ?> <option value="all" <?php selected('', "all"); ?>>All <?php echo $label;?></option> <?php foreach ($posts as $post) { echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>'; }?> <?php echo '</select>'; ?> </p> <?php } add_action('personal_options_update', 'save_pglr_user_pages'); add_action('edit_user_profile_update', 'save_pglr_user_pages'); function save_pglr_user_pages($user_id) { if (!current_user_can('edit_user', $user_id)) return false; //save dropdown update_usermeta($user_id, 'user_custom_page', $_POST['user_custom_page_value']); } 

:そして、私はそれが動作変数を使用しない場合生成されたhtmlがどのようになっているか、$ _GET変数または$ _POST変数を(あなたのフォームで使用しているメソッド属性に応じて) "var_dump"した場合に得られるもの – flynorc

+0

コメントflynorcのThks。 "通知:未定義インデックス:user_custom_xxxxxxx"とデータベース保存: "null" –

+0

2つのファイル(1つのファイルに2つのコード)...メソッドとアクション属性を持つ

要素を生成する部分。そのフォームに