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. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  5. Is it necessary to reset the query after using get_posts()?
  6. How can merge two arrays values in one array and save in database
  7. Get ID of a page containing secondary loop in content
  8. Endless loop with wp_insert_post and wp_update_post
  9. How to update single value in multi dimensional Post Meta?
  10. Add Incrementing ID to each paragraph in the_content
  11. Post Loop not Returning Permalink
  12. Compare two meta key values against each other inside the get_posts array?
  13. Get posts that matches specific terms of multiple custom taxonomies
  14. In a foreach loop, how do I target the last item in the loop?
  15. Looping through WP_Post Object
  16. How do I output a database option that is an array into a get_posts array?
  17. How to add “time” data this?
  18. How to iterate through database until it find a match
  19. Is get_posts() more efficient than The Loop?
  20. Must Use Plugin Causing Query Error
  21. Create a random unique 6 digit number as custom field for custom post type
  22. Advanced Custom Fields: Post Object – Not returning data [closed]
  23. How to get_posts where ‘menu_order’ is more than 0/zero?
  24. How can I add multiple ‘tax_query’ arrays via a loop?
  25. Next and Previous loop
  26. Display the_content on click jquery
  27. What is the maximum number of posts a WordPress loop can work with?
  28. get_*_meta doesn’t always return an array
  29. Why only one post (and no pagination) on this variation of the loop?
  30. Sort posts by most recently modified
  31. A smarter way to display multiple loops inside a page template
  32. How to print out menu names in the foreach loop?
  33. Checking array against author id in loop
  34. Only showing the_date and the_excerpt for first entry in get_posts
  35. Show all posts using the template page and the loop?
  36. Showing sticky posts with get_posts()
  37. query_posts() vs get_posts() multiple loops [duplicate]
  38. How do I paginate a get_posts() request inside another page loop (Paginating childpages)
  39. Problem with displaying HTML content after in_array [closed]
  40. How to hook into container
  41. Both a page loop and posts loop on the same page
  42. How to get The Loop working with $wpdb->get_results()
  43. Get post id within comments loop
  44. Multiple get_posts() queries on one page
  45. Add class to last 3 posts in loop
  46. Array ids post to function have_post
  47. Can’t access PHP array inside script localization from javascript
  48. 404 Template customization | Want 10 recent Post on the 404.php error page apart from the error Notice
  49. Trying to get property of non-object in shortocde
  50. Need to add class to first element in a foreach loop
  51. Foreach displays from last to first
  52. Create Array from custom post type to display a slider
  53. value of metadata is null wrong use of if statement
  54. Get post offset/posts page offset in single post page (outside the loop)
  55. How can I access specific posts brought back by query_posts?
  56. How to create a multidimensional array with multiple loops
  57. Simple foreach loop help needed in wordpress “the loop”
  58. How to get the last category name of a child category?
  59. How to use two meta_compare in an array?
  60. Remove empty terms from array, sort alphabetically, update back to repeating field
  61. How to get user ID’s from multiple usernames?
  62. Only show first image in foreach loop
  63. Get all posts as an array ID => Name
  64. Loop through an array inside a class using foreach [closed]
  65. How to add post meta in while loop?
  66. How to print redux gallery feilds id? [closed]
  67. Sending mail not working correctly
  68. Order a WP_Query by meta value where the value is an array
  69. CPT while loop not working
  70. using update_user_meta to rank users
  71. get_posts() forcing ignore sticky posts on main loop
  72. display post tags on single.php inside loop
  73. Update value of a associative array with update_post_meta
  74. Exclude posts based on an array
  75. loop inside a loop : search for posts in the same category
  76. How to check all items in a loop have post thumbnail?
  77. Add class to every other posts using get_posts
  78. Add 2 values to post__not_in
  79. get_post_meta printing empty fields, but it shouldn’t be
  80. get value from get_post_meta then reuse it in another get_post_meta
  81. in_array not working on dev server but works on localhost
  82. php loop error for count posts in category
  83. Foreach Loop Of Post Types With Nested If Else If Not Completing Process
  84. ACF – Get ID of relationship field in flexible content
  85. Show the subcategory name based on current product
  86. WP post meta – for loop inside for loop
  87. Remove item from post_meta array via AJAX
  88. Check value of post meta within IF statement
  89. Query posts and display all dates in repeater field in chronological order
  90. Efficient way of querying for a “fallback” post?
  91. get_post_meta for Custom Post Type ( CPT )
  92. Get featured image outside the loop using foreach
  93. Create a hierarchical loop at predefined markup requirements
  94. Why loop renders only last metabox from array? [closed]
  95. numberposts not responding to wp_reset_postdata()
  96. WP_Post is not from correct array
  97. Create Customization Controls from Array
  98. How to get files from loop for zip
  99. Inside nested wordpress loop (post & post-type) ” in_array()” function can’t retrieve the matching value
  100. Wishlist Icon in Divi Blog module (loop)
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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