Try to replace
$id[$counter] = get_the_id();
with
array_push( $id, get_the_ID() );
to collect the post id’s into the $id array.
Update:
If you also use $ids instead of $id:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'desc');
$ids = array();
$products = new WP_Query( $args );
if ( $products->have_posts() ) :
while ( $products->have_posts() ) : $products->the_post();
array_push( $ids, get_the_ID() );
endwhile;
endif;
Related Posts:
- How can I fetch loop of post titles via AJAX?
- get product attributes for current product and store it in a variable
- Displaying post per day
- Guidance with The Loop for CMS
- How to add active class to custom menu using while loop and wp_list_pages
- MySQL query performed 4 times inside loop
- Print html when custom field has value inside while loop
- Post from one loop in different containers?
- Custom Loop Pagination on WordPress
- Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
- User Defined order on get_categories?
- get understrap pagination to work with custom query
- When would you use $_post instead of $post?
- How do I display the function the_content in this loop?
- Must Use Plugin Causing Query Error
- How to get to a specific position in wp_query?
- Advanced Custom Fields: Post Object – Not returning data [closed]
- Facebook “like” open graph meta in header.php
- multiple if statements [closed]
- How does the ternary operator work in the wordpress loop post?
- How can I add multiple ‘tax_query’ arrays via a loop?
- Using foreach loop breaks
- Loop with slider (slider not loading)
- How to hide all child pages with post_query?
- How to sort posts based on the value (number) of a post’s metadata?
- Displaying Woocommerce Product Category in WordPress
- How can I add extra word in permalink when someone click download button?
- Blog posts repeat
- Woocommerce : Can’t get product info in loop
- How to set variable, pass it to a partial file and remove it after?
- post thumbnail, conditions, else wont work
- Changing the_posts_navigation() html output
- WP_Query fails despite having 1 post
- wp_query get the 2nd post
- Loop stopped working
- How to loop through all the attached images in a post, and get their url one by one
- Store post content in a php variable and output them using for loop
- How to add div blocks after certain set of post
- Why in my theme I can’t see all the statics content under the posts?
- How do I remove a category from a wordpress loop>
- Show recent posts starting at a specific number archive
- How to get the last category name of a child category?
- WordPress query undefined offset in loop
- How to unlink all posts from tracking same amount of views
- Issue with custom loop in Archive page
- WordPress loop by meta key that is an array? and how loop multiple arrays
- WP Query Leads to 502 Bad Gateway (Timeout Because Query Takes Too Long)
- home.php show blog posts as grid view
- WordPress call post-ID in jquery
- How to extract information from a wp_query result?
- Echo array value
- List all anchor links on a page
- Strip from or something better?
- How to break up php code to avoid echo
- Products listing check if meta checkbox is checked
- Get title of page containing post grid within the posts
- update_user_meta as multiple value but with same meta key
- Responsive loop with 3 columns inside row then 2 columns
- Foreach Loop Of Post Types With Nested If Else If Not Completing Process
- 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
- How can I get my pagination loop to display the correct number of total pages?
- How do if all posts has this category ID then do this
- ERR_TOO_MANY_REDIRECTS / To Force SSL Logins and SSL Admin Access
- Date of last blog update for specific authors only?
- View tag description on page
- Get current_post in reverse order with pagination
- PHP for loop not working as intended
- Insert div after every three posts in home.php [duplicate]
- Display product attributes for current product
- For each 3 posts, show a different post type
- Div Missing In Custom Loop Query
- Using page template to fetch posts in page
- Create Customization Controls from Array
- WP Knowledge Base Theme bug – Subcategories and Articles, Need to change WP_Query
- ACF loop and php formatting
- How to have post count after each listed category
- Inserting A Feed and Sidebar into an HTML Page
- How can I put a custom field as the link of a button shortcode?
- What is the best way to get the first few post from WordPress in different divs using a loop?
- Transient Loop Not working as expected
- Advanced Taxonomy query, with 3 post types
- Custom Meta Box If Else Statement
- Why does a meta_query break this WP_Query?
- Conditional in foreach loop is outputting content twice
- Alternative loop syntax error [closed]
- is there away to hide php code in wordpress not to show in PAGES
- Loop on a wordpress Page instead of content coming from the WP text editor
- How to create a loop where loop changes every post?
- $_html is empty when var dumped
- I want to show image from custom field image on my custom page template
- Count the number of matching post names in foreach loop
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress
- How to show correct td of table tags in wp_query loop
- fetch from an external api call and display results in page
- When a foreach loop is used multiple times in blocks, is there a way to ensure a variable always has a unique value?
- Custom theme on multisite has issues with standard loop output
- How to get the post title inside a custom block in a loop? block.js
- How do I use fields => ids in an array with WP Query?