How to create a multidimensional array with multiple loops

I don’t think this is a restrict WordPress question, but you might try $args = array( ‘posts_per_page’ => ‘-1’, ‘post_type’ => ‘work’, ‘orderby’ => ‘ID’, ‘order’ => ‘DESC’, ); $data = array(‘work’ => array()); $loop = new WP_Query($args); if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); $id = $loop->post->ID; $attachments=array(); if(get_field(‘work’)): while(has_sub_field(‘work’)): $attachment_id = get_sub_field(‘image’); $caption … Read more

Turn get_posts as string into an array for use in theme admin options

This… array(’54’=>’Post #1′,’23’=>’Post #2′, ‘654’=>’Post #3′,) … is not a string. It is an array definition. All you need to do is create an array, which is what you want, and skip the string completely. function post_page_options(){ $post_page_options = get_posts(‘category=orderby=title&order=asc&numberposts=”); foreach( $post_page_options as $value ) : $str[$value->ID] = $value->post_title; endforeach; return $str; }

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?

Couple points: The customize_register action hook actually passes your function the $wp_customize variable, you don’t need to declare it as the global. Just put it as the first argument in your function declaration. The reason your code doesn’t run on the front end of the site is that the customize_register action hook only runs when … Read more

Getting a specific value out of array using get_attached_media

Don’t use the GUID. Despite appearances, that isn’t an URL. Use wp_get_attachment_image_src. Something like: $image = wp_get_attachment_image_src($post_id,’full’); echo $image[0]; Or use wp_get_attachment_url with the post ID. $image = wp_get_attachment_url($post_id); Of course, I don’t know what the actual variable name is that holds your images but you should see the ID in the post objects and … Read more

How can I access specific posts brought back by query_posts?

Why not just use a counter and then conditional logic? $i = 0; while($loop->have_posts()) : $loop->the_post(); if($i === 0){ echo ‘<div class=”a”>’.other_stuff().'</div>’; } elseif($i === 1){ echo ‘<div class=”b”>’.other_stuff().'</div>’; } else { echo ‘<div class=”default”>’.other_stuff().'</div>’; } $i++; endwhile;

Does meta_value (array) work with ‘orderby’?

Researching more, I got it to work with the following code $post = array( ‘posts_per_page’ => 10, ‘post_type’ => ‘post’, ‘tax_query’ => array( ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => ‘category-1’ ), ‘meta_query’ => array( array( ‘key’ => ‘custom-key’, ‘value’ => array(1,100), ‘compare’ => ‘BETWEEN’, ‘type’ => ‘NUMERIC’ ) ), ‘meta_key’ => ‘custom-key’, ‘orderby’ … Read more

How do I create a numbered list with PHP? [closed]

This is pure PHP. $items = get_post_meta($post->ID, “item-specific-meta”); $item_id = 1; $output=””; foreach ($items as $item) { $output .= ‘<label for=”item-‘.$item_id.'”><input id=”item-‘.$item_id.'”/>’.$item.'</label>’; ++$item_id; }

Array in meta key?

A postmeta value can be an array. You save it the exact same. Updating/Getting postmeta will detect if the value is an array/object, and serialize the value so it can be stored in the database. // Saving $users = array( 5, 20, 25, 29, 30 ); $saved = update_post_meta( get_the_ID(), ‘registered_users’, $users ); // Getting … Read more