0

I'm creating a loop which wouldn't output anything visible to the user, but needed to create a calculation later on.

I have:

// posts for the week mon-fri
$week_array = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
    'post_status'      => array('publish'),
    'date_query'       => array(
        'before'    => 'next Saturday',
        'after'     => 'last Monday',
    )
);

$week_count = get_posts( $week_array );

$array01 = array();
$array02 = array();

echo '<h1>' . count( $week_count ) . ' posts this week</h1>';
foreach( $week_count as $post ) {
    setup_postdata($post);
    $a = rand(5,15);

    $meta_array01 = get_post_meta( $post->ID, 'meta_login',  true );
    $meta_array02 = get_post_meta( $post->ID, 'meta_logout', true );


     $array01[$post->ID] = $meta_array01;
     $array02[$post->ID] = $meta_array02;

}
wp_reset_postdata();

My main intention would have been to have something like this outputted:

array(
    [167] => array(
                'meta_login'  => 2016-10-25 18:34:55
                'meta_logout' => 2016-10-25 19:15:12
             ),

    [168] => array(
                'meta_login'  => ...
                'meta_logout' => ...
             )
    ... // and so on for each matching post
);

But I couldn't figure that out, so I thought to merge the two arrays above into one, but when I use array_merge_recursive() I get an array from 0 to the last number of posts, but lose their order.

Effectively, my aim is to calculate the time between the two meta fields.

2

Add 'fields' => 'ids' to skip setting up the post and just create the array you want in the foreach.

$week_args = array(
    'posts_per_page' => - 1,
    'post_type'      => 'post',
    'post_status'    => array( 'publish' ),
    'fields'         => 'ids',
    'date_query'     => array(
        'before' => 'next Saturday',
        'after'  => 'last Monday',
    ),
);

$week_array = get_posts( $week_args );
$week_count = count( $week_array );

printf( '<h1>%s posts this week</h1>', $week_count );

$dates = array();
foreach ( $week_array as $post_id ) {

    $login  = get_post_meta( $post_id, 'meta_login', true );
    $logout = get_post_meta( $post_id, 'meta_logout', true );

    $dates[ $post_id ] = array(
        "meta_login"  => $login ? $login : 0,
        "meta_logout" => $logout ? $logout : 0,
        "dif"         => abs(strtotime($logout) - strtotime($login)),
    );
}

echo "<pre>";
print_r( $dates );
| improve this answer | |
  • Works perfect, and solves quite a few later things I wanted to do! – markb Oct 28 '16 at 2:32
0

Having a look at the get_post_meta WP function https://developer.wordpress.org/reference/functions/get_post_meta/ reveals that using it with the parameter $single set to (bool) 'true' the function returns the meta_value of the requested meta_key. If the parameter $single is set to (bool) 'false' then it returns an array of the meta_value or values (in case there are multiple values matching the query) for the requested meta_key.

So to get your desired output you could go at it like this:

foreach( $week_count as $post ) {
    setup_postdata($post);
    $a = rand(5,15);

     $meta_array[$post->ID] = array( 'meta_login' => get_post_meta($post->ID, 'meta_login',  true), 
                                     'meta_logout' => get_post_meta($post->ID, 'meta_logout', true)
                                    );
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.