I’m assuming your $selected
variable contains an array of values for the currently selected items?
You can’t use selected in this case (with multi select boxes) because it only compares two strings. It won’t test to see if the value is in an array. Instead, use a ternary statement and in_array()
<?php
foreach ($cats as $cat_list ) {
$selected = in_array( $cat_list->cat_ID, $selected ) ? ' selected="selected" ' : '';
?>
<option value="<?php echo $cat_list->cat_ID; ?>" <?php echo $selected; ?>><?php echo $cat_list->cat_name; ?></option>
<?php } ?>
Related Posts:
- Selecting and outputting results from DB with an array
- Struggling with array and foreach loop
- Do not replicate items if they exists in a foreach loop
- How to randomise my menu items?
- Combine two foreach Arrays into one table row
- Obtaining values from objects
- How to get a page array and category array going at the same time?
- get_the_terms, whether I should use string or array as the second parameter?
- How to get the last category name of a child category?
- How to say if meta_value is greater than 0 in an array?
- How to use two meta_compare in an array?
- Any idea how to get something like this to work? [closed]
- Warning: in_array() null given in PHP function
- Why can’t I return a value from $wpdb->get var?
- Foreach loop inside an array_merge
- Loop over Array and get the distinct ids
- Obtaining array item from WordPress revisions output
- How to access or parse key/values that have “string”
- If Array Values Match Another Array’s Values, Then
- Remove empty terms from array, sort alphabetically, update back to repeating field
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Only show first image in foreach loop
- Conditional multidimensional arrays and array_map
- HTML dropdown setting not showing last saved value in the database
- How can I get all values from my array in a loop in php? [closed]
- Sorting dynamic select/dropdown for Contact Form 7 of Modern Tribe Events posts
- Getting posts to exclude from array
- Get all posts as an array ID => Name
- Matching slug terms from one array to those in array of WP_Term objects to output term names
- I’m unable to call img path using single quotes in an array?
- Print specific values stored in a post meta array
- get_terms() returns Trying to get property of non-object error for custom taxonomy
- Filter by field with array value in ACF on WP REST API
- extract serialized array to use for wp-query
- get users search not working with array
- Comapare get_user_meta value
- How can get all users by current user meta?
- Working of foreach loop with array
- Get the id of all images in a post
- Output category list inside array
- how to make an array of post id’s in is_single
- How can I split my query result in 2 arrays?
- Multidimensional Array
- Separate array output into a
- array_rand not working correctly?
- Add key and value to an array in another file
- Get css class of menu item in custom menu structure
- WP Page Options Array
- Dynamically adding filters
- Order a WP_Query by meta value where the value is an array
- Array is not working in Filter?
- Adding option values as an array using a multi selectable select box
- How to put an array in wp user query
- Custom Taxonomy Select Menu: Setting default option value?
- Only load certain artists on this page
- add the value of a variable returned in a while loop [closed]
- Get the values from an array string to work with post__in
- ACF checkbox array is returning numbers, not labels [closed]
- Update value of a associative array with update_post_meta
- return paginate_comments_links() as array
- How to make sure relative URL works when site is not on root domain?
- Get URI from PodPress using PHP
- Inserting a random number into an array [closed]
- Get array value
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- Random element in get_post_meta array
- using images in next/previous_post_link [closed]
- Add a select-option to the default widgets
- update_user_meta duplicates entry
- Finding and removing duplicates within WP Arrays
- Ways of dealing with nested arrays within option
- Using multiple variables to assign categories to an array
- Add formatting to Array
- Compare current post Category in select menu
- Categories Listing and Highlighting current category item
- Custom functions for string data calculations
- Add custom fields from different posts
- Repeat a function with 24hrs gap for n number of days
- MySQL Query Returns Array () In Shortcode
- How to set value selected using select2 jquery plugin
- Problem with inserting multiple images in gallery of each WooCommerce product programmatically
- Convert a column of a table containing an Array as response in HTML
- Database SQL query error
- Sending simple variable on single page to WP JSON
- Create an array with all the links of the years’ archive (of a custom post type)
- Display just one post from a loop of 5 posts?
- Order users by random not working?
- Redux option doensn’t seem when include from functions.php
- in_array not working on dev server but works on localhost
- Taxonomy terms not properly displaying as array
- Creating an array from form inputs before it is posted to the options database
- WordPress, result array of ‘post_name’
- How to make a pagination with dropdown selection?
- How can I show how much water got saved on this order (making use of the number of items bought) on a thank-you-page?
- how to separate an array into different arrays and save them into db?
- Why are array_slice() and array_chunk() not working?
- Retrieve array items without page ID
- How to insert multiple rows and columns in database using array
- Display one random image from Media Library
- How do i fix this “call_user_func_array()” error