There shouldn’t be a foreach
loop inside the while
. The while
loops over posts, so you don’t need another foreach
. The while
loop in this context is what WordPress calls “The Loop”.
See the section of documentation for The Loop on secondary queries here.
So your loop should look like this:
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
<?php the_title(); ?><br>
<?php endwhile; ?>
Related Posts:
- WP_Query vs get_posts
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- WP_Query: query posts by ids from array?
- pre_get_posts with get_posts
- Query Custom Meta Value with Increment
- Identify which loop you are hooking into; primary or secondary?
- Transient pagination not working properly
- meta_query on a date range using an array of values
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- if/else on custom query gives 200 OK when condition not met?
- Order by empty custom field
- Check if loop has any categories?
- How to get list of posts from permalinks?
- Is_single() conditional tag returns null in query
- Transient api Caches confused
- Include current post into loop
- Should I reset $wp_query?
- How to filter query loop block with a search string from the query parameters
- I am officially missing something about transient posts
- how to show more than 1 post into three columns query
- Ordering Posts by parent category, name ascending
- WP_Query secondary query failing
- Loop through multiple custom fields with increasing number
- meta_query dates from an array
- Main loop querying current template’s info only in custom category archive pages, not my posts
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Resetting post data to previous loop in nested loops
- Should we trust the post globals?
- Pagination returns 404 after page 20
- Make a WP Query search match exactly the search term
- Make loop display posts by alphabetical order
- get custom post type by tag
- Group posts by custom field
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- How to get the posts published in last two days using WP_Query?
- get_posts() seemingly ignoring post_type
- Can not switch the queried post in pre_get_posts hook
- Get the post permalink within the loop but without additional DB query
- How to show the posts of some category first, and then all other
- Pagination stops at page 6
- Paginated HTML Sitemap
- How to limit total number of posts in wp query loop?
- Pagination is broken and I need help fixing it
- Query total number of posts
- Custom query for sidebar isn’t returning results
- How to vary post loop results layout and resume?
- Get a post from other loop each n-posts in the main loop
- Loop returning only 1 result
- Query last updated posts (posts updated in the last 24 hours)
- WordPress Loop and $post
- reset to main loop doesnt work
- Some doubts about how the main query and the custom query works in this custom theme?
- Post loop for all taxonomy terms
- Stuck in the query loop
- Query Page Content From Theme Options?
- Only display post if published in last 24 hours?
- How to remove the most recent post from $the_query
- WP_Query loop else statement not executing
- Modify Search Query if original Query gave no results
- How to display the featured image for each post?
- Filter post query to only show direct children of category
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- has_excerpt() not working inside wp_query
- Save (and exclude) posts from loop to use in another loop on page
- Get all posts with empty meta_value
- Check if a post has term inside loop
- WP_User_Query orderby meta_val_num
- Query Taxonomy By Page Title
- Unable to paginate a custom page query
- How to change the default post type over the loop?
- Paginate pages with dynamic query
- WP_Query loop doen’t work with my custom taxonomy
- Determine if ID is page or post and query the ID
- Query pages created by deleted users
- show most viewed post
- Get posts that match defined arrays of tags
- Bootstrap grid while loop
- Order posts by tags count?
- How can I use WP_Query to sort ‘event’ custom post type by date?
- Strange results from WP_Query
- Display posts side by side with custom query
- Is there a way to control both Order By and Order query parameters from one input field
- Meta query doesn’t remove placeholder escape before query
- query all posts published by certain user id
- modifying the loop multiple times with arguments passed through ajax to wp_query
- WP_Query causing links to not work
- Retrieving category pages from subcategory returns empty sets
- How to get query results for the next page
- Iterate through ID’s in loop
- Different number of posts showing in development vs production server
- An archive page without post format (just standard post)
- How to organize a WP_Query’s list of posts by category and display category title?
- Need help setting up a search form for wordpress [duplicate]
- Pull Instagram images into an existing loop?
- Multiple loop for “featured” items returns wrong posts
- Get WP_Query query after execution?
- First post outside of loop, homepage only?
- Show number of posts AND number on current page (cannot make it work)