0

Is there a way to pass an array as a variable out of the wp_query loop? I want to collect post meta from the four posts — next week and the following three weeks. To keep the code clean, I thought it'd be easiest to capture the variable and use the data later in the form.

// Capture next week's week number
$nextweek = date("W", strtotime( "+1 week"));

// Query all custom post types
$menu_args = array(
    'post_type'=>'single_menu',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
    );

    $menu = new WP_Query( $menu_args ); if ( $menu->have_posts() ) : while ( $menu->have_posts() ) : $menu->the_post();

    // Get custom fields
    $menuids = array( get_post_field( 'week', $post->id ) );
    $titles = array( get_post( $post->id )->post_title ); 
    $choosefrom = array( get_post_field( 'menu_listing', $post->id ) );
    $selection = array( get_post_field( $checkout_menu, $post->id ) );

    // Create new array with custom fields  
    $result = array();
    foreach ( $menuids as $id => $menuid ) {
        $result[$menuid] = array(
            'title'  => $titles[$id],
            'offering'  => $choosefrom[$id],
            'menu' => $selection[$id],
            'delivery'    => 0,
        );
    }

    // Return the arrays for specified weeks
    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {
        print_r( $result );
    }
    }

    endwhile;   
    endif;

print_r() returns the correct results, my $results array for the next four weeks. However, when I create a variable and call it outside the loop, like below, I only get the last/fourth $result in the array:

    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {

        $payload = $result;
    }
  }

endwhile;   
endif;

print_r( $payload );

Same thing happens when I create a new array like so:

    $payload = array();
    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {

        $payload[] = $result;
    }
  }

endwhile;   
endif;

print_r( $payload );

If I concatenate the variable like $payload .= $result;, I get ArrayArrayArrayArray returned.

I know I can echo the form inside loop, but would rather take the array as a data variable if at all possible. Am I missing something easy?

4
  • Are you simply looking for the $posts property ...? codex.wordpress.org/Class_Reference/… Commented Dec 6, 2017 at 21:13
  • I am having trouble passing the data array I created out of the endwhile and endif loop. How might the $posts property help me achieve this? Commented Dec 6, 2017 at 21:25
  • Not really sure what your problem is then ... is it maybe simply how to add new elements to an array in a loop, instead of overwriting variables in each iteration ...? If so, that's rather trivial to research, google.com/… Commented Dec 6, 2017 at 21:29
  • Well, thank you for pointing out this should be easy. Turns out my problem was that I needed to put payload = array(); OUTSIDE of the loop. You helped me to stop over-thinking it. Commented Dec 6, 2017 at 22:20

2 Answers 2

1

$result initialized every loop iteration. Your $result contain only one item. Same think for $payload variable. Outside of while loop you have only the last item. Good luck

Sign up to request clarification or add additional context in comments.

Comments

0

Sometimes we can't see straight ... $payload = array(); needed to be placed outside of the wp_query, before $menu_args.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.