Serialized array, grab specific posts with meta_key/meta_value[0]->is_featured

It is possible if you shape your meta query to look for the serialized string like this, assuming your array value is always an integer: $params = array( ‘meta_query’ => array( array( ‘key’ => ‘mediSHOP_product_extras’, ‘value’ => ‘s:11:”is_featured”;i:1;’, ‘compare’ => ‘LIKE’ ) ) ); $query = new \WP_Query( $params ); If the array value is … Read more

How to update serialized post meta?

You must read custom field themeOps first, update index option3 in array and then save/update whole array. $kisiArray = get_post_meta( $post_id, ‘themeOps’ ); $kisiArray[‘option3’] = ‘peach’; update_post_meta( $post_id, ‘themeOps’, $kisiArray ); Update (sort by custom field) $args = array( ‘post_type’ => ‘post’, ‘meta_key’ => ‘themeOps_option3’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ); $query = new … Read more

Passing array of strings to a SQL statement in a WordPress plugin

$wpdb->prepare() accepts a single array of arguments as the second parameter, e.g. $wpdb->prepare( “SELECT * FROM table WHERE field1 = %s AND field2 = %d AND field3 = %s”, array( ‘foo-bar’, 123, ‘[email protected]’ ) ) Therefore you can use an array of placeholders with the SQL command (the 1st parameter), like so: // Create an … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)