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
- MySQL “Or” Condition
- Sorting list of sites from multisite network using wp_get_sites
- How to store the_title() into a variable to reutrn the value, not just echo it
- How to update single value in multi dimensional Post Meta?
- Comma seperated list of ID’s as an array?
- post__in works but also prints the word Array
- if is_singular array not working as expected
- Looping through WP_Post Object
- Using a javascript file to access a get posts array
- Sort a custom post type array numerically
- Generate an array of parent/child page IDs, based on parent page name
- How to add day number and initial to my post graph?
- How do I output a database option that is an array into a get_posts array?
- How to validate register settings array
- Listing and displaying WooCommerce Shipping Zones in the frontend? [closed]
- How do I dynamically generate args for wp_query?
- how to save selection meta data in user profile on frontend
- Working with query_posts ( arrays and query strings)
- How to make drop down post title
- Recent posts with featured image or fallback image with permalink
- Storing Array from returned database query and using the array in a new query
- Custom profile field with birthday. Troubles with
- get_the_tags() not iterating through for/while loop, but will with foreach
- How to save data of an input field to an array
- Stuck with a Custom Field Check box Array
- Compare with WP Meta Query
- How to Translate a string of an array inside a class?
- Modify foreach loop for social sharing links?
- Exclude posts based on meta value
- How to use json_decode a post_meta value? [duplicate]
- Alter required message using comment form api
- How to display line breaked meta values in table?
- Advanced Custom Fields – display label and value only if value entered
- wp_force_remove_style’ not found
- How can I access string value in an array?
- array_filter warning – wp-includes/post.php line 3148
- How to array only one key from another array
- Filtering a function’ output for a new continued function
- add onchange to select in a wp form
- Why is an array created in a function hooked to customize register populated when customizer is loaded but not when the front-end is loaded?
- How to say if meta_value is greater than 0 in an array?
- Any idea how to get something like this to work? [closed]
- Why can’t I return a value from $wpdb->get var?
- If Array Values Match Another Array’s Values, Then
- WordPress loop by meta key that is an array? and how loop multiple arrays
- I’m unable to call img path using single quotes in an array?
- Print specific values stored in a post meta array
- Comapare get_user_meta value
- How can get all users by current user meta?
- Get the id of all images in a post
- Output category list inside array
- return paginate_comments_links() as array
- Inserting a random number into an array [closed]
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- Add custom fields from different posts
- MySQL Query Returns Array () In Shortcode
- 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)
- in_array not working on dev server but works on localhost
- Why are array_slice() and array_chunk() not working?
- Retrieve array items without page ID
- How to display MySQL table data which is stored as an array?
- Show the subcategory name based on current product
- How to filter specific element of an array in wordpress/php?
- get_category_parents to array
- Notice : Array to string conversion on array_intersect user meta
- Remove item from post_meta array via AJAX
- Strange Behavior with update_option
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- PHP array to jQuery array ” Uncaught SyntaxError: Unexpected token < "
- How i can extract data
- Show Tags of Custom Post Type outside of Loop
- Display posts using post ID’s in an array
- wp_set_object_terms not accepting variable array
- Display a single row from nested array of custom field data
- How do I pull post from standard post format?
- Get 3 row ID’s via ARRAY_A
- Reformat data within a nested array
- How to find the function which is outputting Array
- Create Customization Controls from Array
- The writing ( with foreach) of the keys of an array of objects to a new array produces fatal error
- Page doesn’t load
- wpdb select from using array as search parameters
- update_post_meta array issues
- !in_array doesnt recognize category
- Showing all post from all post type in admin backstage
- Accessing array elements (get_pages)
- How to set if meta_value is lower < than other meta_value in a get_posts array?
- Printing out JSON array returned [closed]
- Populate checkboxes from array
- SQL query add only posts with custom field to array
- PHP Use Declared array Variable inside already Declared Array
- Populate Array with values from another arrray [closed]
- Why is my array_diff usage breaking things?
- Adding date and time to the same request