the_post
places the next post object from $query->posts
in the global $post
and calls setup_postdata
, which assumes $post
is a post object, as it tries to access member vars of that object, which is where the errors come from.
In this case $posts
is just an array of IDs instead of post objects. If you want to iterate over the results, you can do a foreach
on $posts
:
$args = array(
'post_type' => 'product',
'fields' => 'ids',
);
$query = new WP_Query($args);
if ($query->have_posts()):
foreach( $query->posts as $id ):
echo 'ID: ' . $id;
endforeach;
endif;
Related Posts:
- Why is the loop not empty on some 404s?
- Multiple custom fields for ‘orderby’ in ‘WP_Query’
- How to order by post_status?
- How to paginate wordpress [gallery] shortcode?
- How to Access Global $multipage or Global $numpages outside the loop?
- How can I use WP-CLI commands without –allow-root
- Merging a complex query with post_rewind and splitting posts into two columns
- Pagination Not working on Home Page with 2 Query
- WP_Query in a shortcode
- ‘posts_where’ filter not applying ‘WP_Query’ in `wp_ajax`
- meta_query not working properly
- Get all posts without tags
- Filter wp_query to search post title in function
- Two queries on the same page with pagination
- Using tax_query reverses my post_type argument in a custom WP_Query
- Best approach to create Hot and Trending sections
- creating wp query with posts with specific category
- get complex results set according to category structure
- Searching through different categories on different pages code is not working
- Setup of taxonomy term template pages
- Ignore post by meta value in the main query
- Display posts in 3 days chunk
- how to get the id for a post from within a post and return post meta for it
- WP_Query’s “request” SQL Query
- How to do a query on custom taxonomies that is uncategorised?
- How to make this WP_Query run faster on a WordPress website?
- Order taxonomy terms in alphabetical order
- is_search was called incorrectly
- Empty tax_query array returns an empty array
- How do I sort posts with multiple pages
- Sorting By Custom Posts With Attachments
- How to choose between hooking into per_get_posts or into parse_query
- Make (post) query a clickable link
- How to set pagination to work with ‘/page/’?
- How to do meta_query for attachments?
- How can I fix: “Notice: Undefined offset: 0”?
- Most commented last 24h, week, month, year and all time – posts_where
- WP_Query Pagination on multiple-loop page breaks WP or doesn’t show up
- Why isn’t my `meta_query` array functioning properly?
- How to duplicate 5 posts out of WP_Query results?
- Import wp users via one click demo option
- WP_query sorting can’t sort danish letters (æ, ø, å)
- Dynamic User Id
- Slow queries on a huge database
- Multisite pagination issue by multi query archive 404
- get_post_meta slowing down my page load (in a plugin)
- “tax_query” parameter is ignored when “p” parameter is set in WP_Query
- Hide elements outside loop based on query
- WP Query crashes on more than ~ 2000 posts
- How can i share WP_Query between multiple functions?
- sort and display posts by custom field (wp-query and the loop))
- I need help using pre_get_comments to limit comments in the comments admin screen
- WP_query only displays one of my custom post type entries
- Issues with search after added meta_query
- How to load a script code only in posts?
- Woocommerce: order posts by meta key
- Modify query posts
- query post by author gender
- Display all attached image of every post of custom post type and link to original post
- How do I add an item to the WP admin menu?
- Multiple meta query from array
- Find only those galleries with images
- WQ Query post meta date comparison
- Filter subpages in while loop from WP Query object
- Reciving Error 404 blog/page/2/
- WP Query Order By Multiple Values
- Error: Attempt to read property “ID” on null
- Unrelated post showing in WP_Query
- Custom meta_query order for Elementor based on post meta key [closed]
- wordpress pagination wont render on custom page template even though its right under the loop like the documentation says so
- ElasticPress is (aparently) messing with my search filters
- show related articles in single.php template
- Order by Date Custom Field
- WP_Query showing all posts, except from category X, unless it’s also in Y
- How to query search only for post titles?
- Shortcode attributes causes 500 error after updating the page
- Show posts by category – category ID not working
- WP_Query to Retrieve Posts from WooCommerce Products that are Only in Catalog
- Search Filter With Custom Taxonomy and Custom Fields : How do I handle it via plugin?
- query_posts based on a meta_key
- wp_get_attachment_image not to get the post_thumbnaill
- Query with two posttypes and two different meta_keys
- Sort by meta key within same day
- Duplicates When using Nested WP_Query
- meta_query not working with the_content()
- Show portfolio items in template – how?
- Pagination in custom query in a category page
- WordPress WP_Query Array Custom search via taxonomies
- Problem with WordPress 4.0 and $wp_query
- Add category and user name to recently updated post list
- Pagination in WP query with transient API
- WP Paginate not working properly
- How to get child pages and add unique class only to active page?
- Main loop querying current template’s info only in custom category archive pages, not my posts
- How can I display sticky posts at first in wp_query?
- The permalink redirecting to current article on single.php page
- Variations as Single Products [closed]
- Bulk delete comments, foreach loop
- Send a variable with a URL and get a variable with Wp query
- Showing Counts on Comment List with Custom Query