Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Output meta into arrays

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 );

Related Posts:

  1. post__in – Placing content from a foreach loop inside of an array
  2. Get all posts including sticky ones with get_posts(),setup_postdata(), and foreach loop?
  3. WordPress loop by meta key that is an array? and how loop multiple arrays
  4. Is it necessary to reset the query after using get_posts()?
  5. How can merge two arrays values in one array and save in database
  6. Get ID of a page containing secondary loop in content
  7. How to update single value in multi dimensional Post Meta?
  8. Add Incrementing ID to each paragraph in the_content
  9. Post Loop not Returning Permalink
  10. Compare two meta key values against each other inside the get_posts array?
  11. Get posts that matches specific terms of multiple custom taxonomies
  12. In a foreach loop, how do I target the last item in the loop?
  13. How to add “time” data this?
  14. How to iterate through database until it find a match
  15. Must Use Plugin Causing Query Error
  16. Create a random unique 6 digit number as custom field for custom post type
  17. Advanced Custom Fields: Post Object – Not returning data [closed]
  18. How to get_posts where ‘menu_order’ is more than 0/zero?
  19. How can I add multiple ‘tax_query’ arrays via a loop?
  20. Next and Previous loop
  21. Display the_content on click jquery
  22. get_*_meta doesn’t always return an array
  23. Why only one post (and no pagination) on this variation of the loop?
  24. Sort posts by most recently modified
  25. A smarter way to display multiple loops inside a page template
  26. How to print out menu names in the foreach loop?
  27. Checking array against author id in loop
  28. Only showing the_date and the_excerpt for first entry in get_posts
  29. Showing sticky posts with get_posts()
  30. query_posts() vs get_posts() multiple loops [duplicate]
  31. How do I paginate a get_posts() request inside another page loop (Paginating childpages)
  32. Problem with displaying HTML content after in_array [closed]
  33. How to hook into container
  34. Both a page loop and posts loop on the same page
  35. How to get The Loop working with $wpdb->get_results()
  36. Get post id within comments loop
  37. Add class to last 3 posts in loop
  38. Array ids post to function have_post
  39. Can’t access PHP array inside script localization from javascript
  40. 404 Template customization | Want 10 recent Post on the 404.php error page apart from the error Notice
  41. Trying to get property of non-object in shortocde
  42. Need to add class to first element in a foreach loop
  43. Foreach displays from last to first
  44. value of metadata is null wrong use of if statement
  45. How to create a multidimensional array with multiple loops
  46. Simple foreach loop help needed in wordpress “the loop”
  47. How to get the last category name of a child category?
  48. How to use two meta_compare in an array?
  49. Remove empty terms from array, sort alphabetically, update back to repeating field
  50. How to get user ID’s from multiple usernames?
  51. Only show first image in foreach loop
  52. Get all posts as an array ID => Name
  53. Loop through an array inside a class using foreach [closed]
  54. How to add post meta in while loop?
  55. How to print redux gallery feilds id? [closed]
  56. Sending mail not working correctly
  57. Order a WP_Query by meta value where the value is an array
  58. CPT while loop not working
  59. using update_user_meta to rank users
  60. get_posts() forcing ignore sticky posts on main loop
  61. display post tags on single.php inside loop
  62. Update value of a associative array with update_post_meta
  63. Exclude posts based on an array
  64. loop inside a loop : search for posts in the same category
  65. How to check all items in a loop have post thumbnail?
  66. Add class to every other posts using get_posts
  67. Add 2 values to post__not_in
  68. get_post_meta printing empty fields, but it shouldn’t be
  69. get value from get_post_meta then reuse it in another get_post_meta
  70. ACF – Get ID of relationship field in flexible content
  71. WP post meta – for loop inside for loop
  72. Remove item from post_meta array via AJAX
  73. Check value of post meta within IF statement
  74. Query posts and display all dates in repeater field in chronological order
  75. Efficient way of querying for a “fallback” post?
  76. get_post_meta for Custom Post Type ( CPT )
  77. Get featured image outside the loop using foreach
  78. Create a hierarchical loop at predefined markup requirements
  79. Why loop renders only last metabox from array? [closed]
  80. numberposts not responding to wp_reset_postdata()
  81. WP_Post is not from correct array
  82. Foundation modal in wordpress php loop
  83. Best way to check if a post with specific meta exists
  84. ACF field not appearing correctly in loop
  85. WP update_post_meta link loop
  86. Humanmade | Custom meta box class: How to display a repeatable meta box group?
  87. Can’t print out returned value
  88. get_posts and wp_autop (remove filter)
  89. Display articles related to a custom field on a page
  90. Transient Loop Not working as expected
  91. Need some help with “the_widget()” function
  92. Is there a way to target only images within the loop?
  93. Show last post from multiple categories using wp_list_categories
  94. WordPress loop: Display if posts exist
  95. How can I access ‘key’ and ‘value’ from an array in a for loop?
  96. Eliminate duplicates in a foreach loop [closed]
  97. Wrap every 2 divs in row – for each loop [closed]
  98. Question about custom plugin
  99. posts_per_page – Repeats only first posts ‘post__in’ array
  100. How to get files from loop for zip
Categories loop Tags array, get-posts, loop, php, post-meta
Do Not Understand → Rule No. 4: Making Data Safe Is About Context [closed]
Proper way of establishing custom landing pages in WordPress

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress