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 to loop over custom fields in a page template?
- Alter secondary loop to exclude posts from current page category
- Store loop into array
- Using a javascript file to access a get posts array
- Generate an array of parent/child page IDs, based on parent page name
- How do I output a database option that is an array into a get_posts array?
- Check if term object is in array
- Show Custom Taxonomy Title
- How to exclude specific category from the get_the_category(); array
- spliting posts into two columns
- How to Display a Single Post Excerpt
- $wpdb->flush(); breaks the loop
- Using a variable in is_page(array())
- Remove duplicates – array_unique()
- Recent posts with featured image or fallback image with permalink
- Storing Array from returned database query and using the array in a new query
- Changing layout with wp_customise
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- get author_name from queried post
- Exclude posts based on meta value
- 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
- post thumbnail, conditions, else wont work
- Advanced Custom Fields – display label and value only if value entered
- wp_force_remove_style’ not found
- How can I access string value in an 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
- Why in my theme I can’t see all the statics content under the posts?
- How to say if meta_value is greater than 0 in an array?
- How to unlink all posts from tracking same amount of views
- If Array Values Match Another Array’s Values, Then
- Why my loop isn’t working? [closed]
- I’m unable to call img path using single quotes in an array?
- Problem with custom loop and wp_list_pluck [closed]
- Get the id of all images in a post
- Output category list inside array
- Separate array output into a
- How to wrap every 3 posts in a div (and close the last div too) [closed]
- Dividing the loop to style post differently
- Update value of a associative array with update_post_meta
- First post article different on Archives template
- How to add title attribute to archive items
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- Strip from or something better?
- Add custom fields from different posts
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- MySQL Query Returns Array () In Shortcode
- How to Create Carousel Indicators in PHP Loop using wp_get_attachment_url function?
- Get title of page containing post grid within the posts
- Is there anyway I can call the year once?
- How to display MySQL table data which is stored as an array?
- Foreach Loop Of Post Types With Nested If Else If Not Completing Process
- Where does php code to load data go?
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- cURL needing to loop through all “next_page”
- Display page content in different sections – based on page break block?
- Filtering custom posts by using checkboxes for taxonomy in an ajax loop always gives server 500 error
- Latest posts feed with a specific post always first
- How can I get my pagination loop to display the correct number of total pages?
- Notice : Array to string conversion on array_intersect user meta
- Get current_post in reverse order with pagination
- How fix error in the WordPress loop?
- Display posts using post ID’s in an array
- Display a single row from nested array of custom field data
- Div Missing In Custom Loop Query
- Loop 1 user randomly
- Using page template to fetch posts in page
- Non-array argument in array_merge()-function [closed]
- Problems With Query and/or Template Part and/or PHP
- Two loops in one function
- How to display thumbnail if post is assigned one otherwise not
- Pagenav Not appearing on custom Template
- Error trying import one category on page
- Remove the_content From Loop
- Adding if statement to content for homepage
- Tables not showing divs and loop/php items
- Showing all post from all post type in admin backstage
- Pagination in category.php not functioning
- Use php array outside his function
- For each loop will not append to the_content hook
- How to handle parent and child pages?
- How do I remove blog entries from home page Twenty Eleven child theme?
- How to create a loop that will display one post and stop?
- Pagination not working properly
- Grabbing value of input field inside of array
- Custom WordPress Function – Adding items from Foreach Loop into an array and Updating Field based on array of IDs (ACF + WooCommerce)
- PHP – Converting elements to actual values
- How do I display WooCommerce products in my query to rows of 3?