{"id":1299686,"date":"2009-12-07T03:28:46","date_gmt":"2009-12-07T03:28:46","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/php-form-select-categories\/"},"modified":"2016-08-19T14:52:01","modified_gmt":"2016-08-19T14:52:01","slug":"php-form-select-categories","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/php-form-select-categories\/","title":{"rendered":"PHP Form: Select Categories"},"content":{"rendered":"<p>Refining my options panel and I want to offer the user the option to select certain categories from a drop down. This works &#8211; and even saves. However after the save the drop down list doesn&#8217;t &#8220;remember&#8221; the selection and if you save again changes are lost.<\/p>\n<pre><code>&lt;select class=&quot;aselect&quot; id=&quot;&lt;?php echo $value[&#039;id&#039;]; ?&gt;&quot;  name=&quot;&lt;?php echo $value[&#039;id&#039;]; ?&gt;&quot;&gt;\n\t&lt;option value=&quot;0&quot;&gt;&lt;?php _e(&#039;Select a Category&#039;, &#039;thetimes&#039;); ?&gt;&lt;\/option&gt;\n\t\t&lt;?php\tglobal $options;\n\t\t\t\t\tforeach ($options as $value) {\n\t\t\t\t\t\tif (get_settings( $value[&#039;id&#039;] ) === FALSE) { $$value[&#039;id&#039;] = $value[&#039;std&#039;]; } else { $$value[&#039;id&#039;] = get_settings( $value[&#039;id&#039;] );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t$categories = &amp;get_categories(&#039;type=post&amp;orderby=name&amp;hide_empty=1&#039;);\n\n\t\t\t\t\tif ($categories) {\n\t\t\t\t\t\t$find_select = $tt_catbox_1;\n\n\t\t\t\t\t\tforeach ($categories as $category) {\n\t\t\t\t\t\t\t$selected = (in_array($category-&gt;cat_ID, $find_select)) ? &#039; selected=&quot;selected&quot;&#039; : &#039;&#039;;\n\t\t\t\t\t\t\techo &#039;&lt;option value=&quot;&#039; . $category-&gt;cat_ID . &#039;&quot;&#039; . $selected . &#039;&gt;&#039; . $category-&gt;cat_name . &#039;&lt;\/option&gt;&#039; . &quot;\\n&quot;;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t?&gt;\n&lt;\/select&gt;<\/code><\/pre>\n<p>$tt_catbox_1 contains the category ID <\/p>\n<p>I&#8217;ve Googled and found the above code, however I can&#8217;t get the &#8220;SELECTED&#8221; to work quite right. I think I need to pull from the database the settings. How do I do that?<\/p>\n","protected":false},"template":"","class_list":["post-1299686","topic","type-topic","status-closed","hentry","topic-tag-form","topic-tag-options-panel","topic-tag-php","topic-tag-select"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1299686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1299686\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=1299686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}