There is a difference because you are not using the_post
function in the first example. What this function does is it calls setup_postdata
function, which sets up all the globals needed for other functions to work. You can call it manually, just at the beginning of your foreach
loop, like this: setup_postdata( $post )
. In addition it is possible that you would also need to call a global $post;
before your foreach
loop.
Related Posts:
- How to list some posts first in the loop based on post id
- How to order posts tag by tag?
- How do I run through a WordPress loop called from a filter function?
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- has_excerpt() not working inside wp_query
- How to split a post and intercalate elements from a loop
- Display specific page (that is child) content on parent page
- Filtering by multiple conditions in the loop
- relation OR instead of AND – Filtered term ID’s in loop
- Insert wp_query after the_content with plugin (filter the_content won’t work)
- WP_Query doesn’t works inside loop
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Changing the default wp_search_stopwords
- How to display the featured image for each post?
- Assign custom parameter to each post in query
- How to provide meta_key array to wp_query?
- Wp_query with 2 meta keys and array of meta values
- Adding a html class based on post count
- Check for the main query from the template
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- Trying to retrieve random post, getting a page
- Filter post query to only show direct children of category
- Posts in loop all show the same author when there are many authors
- Exclude recently updated post from custom WP_Query using multiple loops
- get_posts output always same post
- Multiple Rows in a using wp_query
- How to use WP_Query() on single.php?
- how can I get seperate the HTML in the_content(); output?
- sort and display posts by custom field (wp-query and the loop))
- How to create custom query by keyword in post title?
- difference between querying database and using the loop
- Is it possible to add an argument to a custom function added to a filter hook?
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- Conditional operator OR not working with custom fields
- Loop increase in while loop not working
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- posts archive page – closing WP_Query loop correctly [closed]
- Create a page template for “top rated posts” but show full content and not just a list
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- Change AJAX filter from POST to GET for URL Parameters
- Should I reset $wp_query?
- Meta query compare for ID’s greater than specific ID
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- Using post_where filter only on main query NOT sidebard new WP_Query
- Filtering posts by archive showing all years
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- How does WP generate the default $query in WP_Query based on the URL?
- Continue or break the while loop
- How to filter query loop block with a search string from the query parameters
- Differentiate Nested WP_Query from Parent
- WP_Query – How to get all posts of specific days of week by custom field date?
- inserting content of 1 Post to in another with a template hierarchy
- how to get category`s slug in WP_Query loop?
- Loop posts based on permalink term
- Save (and exclude) posts from loop to use in another loop on page
- Get all posts with empty meta_value
- Multiple wp_query on archive page
- How to remove only the latest sticky post from the loop
- Expecting statement error from php loop using ACF plugin
- Loop 3 posts column wise and continue the loop into same columns
- Retrieve select tag custom values from array and display them in current page with wp_query?
- How to use a dropdown to filter posts by custom field
- How I can repeat 2 HTML templates in a WordPress Query?
- PHP – Loop custom post type categories within jQuery Tabs
- Check if a post has term inside loop
- How to create a widget like this?
- Remove post type filter added by the plugin in the final query
- How do I sort this custom list of sticky posts
- WP_Query with one category in args shows other categories
- Query only displays one page_id
- How to Order By Two Custom Fields?
- Different Loop for tag pages?
- custom page with post content using read more
- Ordering posts by custom field and grouped by month
- Pagination for custom loop on custom page template is not displaying anything
- Does putting queries within loops cause an issue on WordPress?
- Add parameter to first post only
- Hiding carousel indicators if there’s only one post
- Query Taxonomy By Page Title
- How to access $wp_query object from array of posts given by get_posts?
- Function using get_posts() with tax_query not working when called from functions.php
- Display all attached image of every post of custom post type and link to original post
- 2 queries with counters
- Get posts with no tags?
- wp-query, pull children of parent page
- Why is get_the_excerpt returning full content
- Can’t order the query result as given posts id’s array
- Writing less unnecessary code with WordPress
- Does query_posts have an effect on get_the_category?
- Second loop pagination changes URL, not content
- How to change the default post type over the loop?
- I am officially missing something about transient posts