WP_User_Query order by meta_key that is an array

You could save the values with the separate user meta keys: x1, …, x6, points instead of the array: achievements to simplify your WP_User_Query() usage and not having to deal with serialized arrays as user meta values. But I guess you’ve already considered this setup and wanted to avoid it 😉 A compromise would be … Read more

Create an array from an array

You can split your array using the following $array = array(1,2,3,4,5,6); $number_of_elements = 3; $count = count( $array ); $split = array(); for ( $i = 0; $i <= $count – 1; $i++ ) { $slices = array_slice( $array, $i , $number_of_elements); if ( count( $slices ) != $number_of_elements ) break; $split[] = $slices; } … Read more

Using a javascript file to access a get posts array

Yep, you’re looking for wp_localize_script(). Do away with the global. add_action( ‘wp_enqueue_scripts’, ‘wpse_enqueue_scripts’ ); function wpse_enqueue_scripts() { wp_enqueue_script( ‘wpse-main’, get_template_directory_uri() . ‘/path/to/script.js’, array(), false, true ); wp_localize_script( ‘wpse-main’, ‘wpseVars’, array( ‘postsLoop1’ => json_encode( /* first post array */ ), ‘postsLoop2’ => json_encode( /* second post array */ ), ‘postsLoop3’ => json_encode( /* third post array … Read more

Get (echo) all role names assigned to user

I assume you need a foreach loop and echo each roles name separately. Here’s how to do it: global $wp_roles; $user_role = get_userdata($user->ID)->roles; // Check if there is any role for this user if( $user_role ) { foreach ( $user_role as $key => $role ) { echo $wp_roles->role_names[ $role ]; // Add a seperator except … Read more

Get posts id in array by meta value and key

Use the meta_key, meta_value, and fields parameters. Example using get_posts(): (Take note though, get_posts() ignores or doesn’t include sticky posts.) $meta_key = ‘hide_rss’; $meta_value=”yes”; $post_ids = get_posts( [ ‘meta_key’ => $meta_key, ‘meta_value’ => $meta_value, ‘fields’ => ‘ids’, ] ); Or using new WP_Query(): $meta_key = ‘hide_rss’; $meta_value=”yes”; $q = new WP_Query(); $post_ids = $q->query( [ … Read more

array_map() for sanitizing $_POST

It’s probably not a great idea. Firstly, if you’ve got other field types then you should probably use more appropriate functions. For example, textarea fields should be sanitised with sanitize_textarea_field(), and color pickers should be sanitized with sanitize_hex_color(). You should also consider that $_POST likely also contains fields that you don’t want to save, such … Read more

How do I update a specific object in an array, in user meta?

You can just assign a new value to the 1h_userbadge_comments25 key. Like so… <?php $meta_value = get_user_meta($user_id, ‘lh_userbadges’, false); // just assign this key a new value $meta_value[‘lh_userbadge_comments25’] = 25; Then just save it again. <?php update_user_meta( $user_id, ‘lh_userbadges’, $meta_value ); Whether or not storing all of those values as an array is the correct … Read more

Saving an array in a single custom field

You can json_encode() it which will make it a string that you can put into a custom field then just ensure you json_decode() it to bring it back to an object or json_decode($data, true) to bring it back as an array $arr = array(“Accounting”=>”Peter”, “Finance”=>”Ben”, “Marketing”=>”Joe”); update_post_meta($post_id, “my_custom_meta_key”, json_encode($arr)); $json_data = get_post_meta($post_id, “my_custom_meta_key”, true); // … Read more

Need to get specific data from array

You should be able to rewrite what you have about and also use get_permalink as @Benoti stated while omitting the $split array. get_permalink accepts either the Post ID or a post object. global $rental; $images = get_children( array( ‘post_parent’ => $rental_id, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => ‘ASC’, ‘orderby’ => … Read more