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
- Multiple queries and pagination
- When should you use WP_Query vs query_posts() vs get_posts()?
- Can I force WP_Query to return no results?
- Resetting post data to previous loop in nested loops
- order by numeric value for meta value
- Should we trust the post globals?
- Query multiple custom post types in single loop
- Some doubts about how the main query and the custom query works in this custom theme?
- Wp get all the sub pages of the parent using wp query
- Get post count of current loop when using multiple queries on one page
- Multiple WP_Query loops with Pagination
- Return only Count from a wp_query request?
- Using a custom WP_Query with get_template_part loop
- How to query for most viewed posts and show top 5
- Loop within a loop?
- WP_Query and next_posts_link
- Show posts without term
- WP_Comment_Query pagination, delving into the unknown
- Display products from specific category in shop page
- How to list some posts first in the loop based on post id
- How to use a custom post type as front page?
- Pagination returns 404 after page 20
- Make a WP Query search match exactly the search term
- Make loop display posts by alphabetical order
- Use WP_Query with have_posts()?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Sort posts by category name and title
- get custom post type by tag
- How to get order of posts?
- Order posts by ID in the given order
- How to uniquely identify queries?
- What is the most efficient way of querying posts based on visits and date for current day?
- Custom WP_Query order by post_meta and (author) user_meta
- Query WooCommerce orders where meta data does not exist
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- get_template_part in for loop
- Get array of posts from the current archive page loop
- WP_Query for WooCommerce Products
- How-to exclude terms from the main query the most performant way?
- Pagination with WP_Query is buggy – working for some pages, but not the others
- Get the number of posts from the current page results
- Executing Queries in tag.php
- Display posts the match taxonomy term linked from wp_list_categoies?
- How can I save an array from a random post sequence for later use?
- How to place a loop within another loop?
- WordPress Custom Query to show posts from last x years
- author.php with ACF and CPTs
- How to order posts tag by tag?
- “pre_get_posts” firing on every query
- Show two random posts from custom post type
- Add the “active” class only to the first loop item in a WordPress query [closed]
- When should you use wp_reset_postdata vs wp_reset_query?
- Loop through all tags & output posts in alphabetical list
- Get posts by meta data OR title
- Pagination with 5 posts per page
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- In loop: posts have thumbnail AND other variables
- How to make an activities stream mixing posts and comments?
- Duplicate Queries
- Group posts by custom field
- How to query for a week using key => value WP_Query argument notation?
- WordPress Number of Posts Not Changing With posts_per_page
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- How to order category.php loop by ‘meta_value’?
- Pagination/infinite scroll with WP_Query and multiple loops
- Show only oldest post by author
- Order posts by tags count?
- Merging a complex query with post_rewind and splitting posts into two columns
- Sort posts by custom taxonomy name
- Add inline HTML to posts published within last 24hrs
- SQL query equivalent to WP User Query