get_the_ID
gives you the ID of the current post, but at no point during that loop do you change what the current post is. It’s always the last post processed in the previous loop because that’s the last time you called the_post()
. This is why you always get the same ID.
Instead, store the ID:
$results[$key]['ID'] = $value->ID;
Then echo that instead:
foreach ($results as $key => $value) {
echo $value['ID'];
}
Another important issues is that you’re using $results[$key]
but you never assign it an initial value, and assume it’s an array, you should initialise it using $results[$key] = arrayy();
first
Related Posts:
- Looping through WP_Post Object
- Must Use Plugin Causing Query Error
- How can I add multiple ‘tax_query’ arrays via a loop?
- Checking array against author id in loop
- How to get the last category name of a child category?
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Only show first image in foreach loop
- Get all posts as an array ID => Name
- in_array not working on dev server but works on localhost
- ACF – Get ID of relationship field in flexible content
- Show the subcategory name based on current product
- Create Customization Controls from Array
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Checkbox doesn’t stay checked, conten
- post thumbnail, conditions, else wont work
- Widgets in the loop if active
- Alter required message using comment form api
- List posts that don’t have a specific tag?
- Changing the_posts_navigation() html output
- Can we count the WordPress Loop
- Advanced Custom Fields – display label and value only if value entered
- Get post id within comments loop
- meaning of (array)function()
- Syntax error when I try to insert my loop into an unordered list? [closed]
- WP_Query fails despite having 1 post
- Grouping posts by a custom meta value
- wp_force_remove_style’ not found
- Add content after the first post in WP Loop
- wp_query get the 2nd post
- Loop stopped working
- How to get post ID in a Page?
- How to fix this warning:call_user_func_array() expects exactly 2 parameters, 1 given in D:\wamp\www\…….\wp-includes\class-wp-hook.php on line 286
- How can I access string value in an array?
- How to use if statement in an array? [closed]
- Custom Pagination is Broken
- Display an image if odd number of posts in grid
- How to loop through all the attached images in a post, and get their url one by one
- How to array only one key from another array
- I’m trying to create an array with a foreach loop, but the array only stores the last item [closed]
- Filtering a function’ output for a new continued function
- PHP Warning: strip_tags() expects parameter 1 to be string?
- Output meta into arrays
- Store post content in a php variable and output them using for loop
- How to add div blocks after certain set of post
- Get html data with javascript to php array and store to wordpress database
- Echo a shortcode div after every 3 posts
- How to avoid duplicates when creating recent network posts
- How to overwrite orderby with add_query_var
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- How do I create a numbered list with PHP? [closed]
- Why in my theme I can’t see all the statics content under the posts?
- 2nd page displaying the exact same posts as my first page (minus the very first post)
- 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?
- Is the 404 page automatically displayed if a loop returns nothing?
- How do I remove a category from a wordpress loop>
- How to use array in function to get only value I want
- Error while setting role
- Get categories names as an array to use it in theme settings
- variable value disappears in a second loop
- Last class on last headline?
- Show recent posts starting at a specific number archive
- get_the_ID() in the footer returns wrong value
- How to say if meta_value is greater than 0 in an array?
- How to use two meta_compare in an array?
- Warning: in_array() null given in PHP function
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- WordPress query undefined offset in loop
- Foreach loop inside an array_merge
- How to pass a variable to get_template_part that’s updated every time the template part is called?
- How to unlink all posts from tracking same amount of views
- Issue with custom loop in Archive page
- Loop over Array and get the distinct ids
- How to access or parse key/values that have “string”
- If Array Values Match Another Array’s Values, Then
- Remove empty terms from array, sort alphabetically, update back to repeating field
- Why my loop isn’t working? [closed]
- use loop to return blog details
- Conditional multidimensional arrays and array_map
- How to get current post category details inside “loop”?
- How can I get all values from my array in a loop in php? [closed]
- How to pass current loop data into nested loop
- Getting posts to exclude from array
- How to get post ID from button and pass title to Modal
- WP Query Leads to 502 Bad Gateway (Timeout Because Query Takes Too Long)
- I’m unable to call img path using single quotes in an array?
- home.php show blog posts as grid view
- Filter by field with array value in ACF on WP REST API
- extract serialized array to use for wp-query
- Problem with custom loop and wp_list_pluck [closed]
- WP_Query multiple post results
- Convert Featured products loop into Recent products loop?
- Wpdb get->results to out the the month from the db
- How to add post meta in while loop?
- get users search not working with array
- Echoing a CSS class based on category of post in a list
- Display child page content of a specific parent on home page
- Comapare get_user_meta value
- How can get all users by current user meta?
- Get the id of all images in a post
- Output category list inside array