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
- Some doubts about how the main query and the custom query works in this custom theme?
- Using a custom WP_Query with get_template_part loop
- Loop within a loop?
- ajax category filter
- Display posts the match taxonomy term linked from wp_list_categoies?
- WordPress Custom Query to show posts from last x years
- Loop through all tags & output posts in alphabetical list
- Pagination with 5 posts per page
- How to support multiple search terms query within one process?
- Merging a complex query with post_rewind and splitting posts into two columns
- Pagination Not working on Home Page with 2 Query
- meta_query not working properly
- How to get any tag ID
- Exclude post on loop by multiple meta key value
- Sort by meta key on archive page
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Query sticky posts with thumbnails
- Show all parents and children in custom post type in right order
- Why is this coming back as null? Thats wrong. There is one post
- Ignore post by meta value in the main query
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- What’s the point of the query_vars filter?
- How to retrieve current page WP_Query arguments?
- How to limit WP_Query to one result on the loop?
- Prevent duplicate post from dynamic multiple loops
- When to add_filter() to Custom Query
- if/else on custom query gives 200 OK when condition not met?
- Multiple Loops Inside a Function
- Random loop with code to prevent duplicate output returns no output at all from time to time
- How to use offset in WP_Query
- How to use filter hook “the_posts” with a function that refers to $this?
- Multiple loops without repeating content
- Can I set my WP_Query to be a Main Query?
- How can I filter by slug depending on language selection
- Filtering posts from different categories into different section by doing WP_Query only once
- WP_Query with different postmeta filter for each categories
- Slow WP_Query with ‘OR’ on meta_query
- How to make search for posts using get method?
- How to get list of posts from permalinks?
- Custom taxonomy.php not working
- different for loop if screen resolution < 1000px
- Transient api Caches confused
- sort and display posts by custom field (wp-query and the loop))
- Conditional operator OR not working with custom fields
- Get posts that do not have the same tags as current
- Should I reset $wp_query?
- Exclude a Woocommerce product from WP_Query
- WP_QUERY wrong ammount of posts
- inserting content of 1 Post to in another with a template hierarchy
- Expecting statement error from php loop using ACF plugin
- How do I sort this custom list of sticky posts
- Query only displays one page_id
- Pagination for custom loop on custom page template is not displaying anything
- How to access $wp_query object from array of posts given by get_posts?
- Display all attached image of every post of custom post type and link to original post
- Writing less unnecessary code with WordPress
- I am officially missing something about transient posts
- How do I get the content of a custom instance of WP_Query?
- $post in wp_query?
- meta_query weird behaviour, static int will work, but not user data
- How do I display main query posts in random order using add_filter
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- ACF: How can I publish values of ACF fields in a loop while using wp_query?
- Can I alter the main loop to ‘orderby’ a custom callback?
- Show a message if there are no active posts in category
- WooCommerce | AJAX | Product Pagination | Help me implement Ajax Pagination
- Problem with my loops
- display the children of the post using the current page as the main parent
- Display 3 levels of categories on page
- Paging works correctly on local version, but not live?
- get term objects and post objects in query
- Custom post type loop error: Trying to get property of non-object
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- Display three sequential posts on each page load, without repeating previous
- display post multiple times based on array of dates
- How to make a wordpress loop file that displays posts based on certain conditions
- Custom loop – Isolating post meta output depending on current query taxonomy terms
- Issue in If else condition [closed]
- Pagination not working with WP_QUERY
- Add custom WP_Query after the first 3 posts
- How do I display posts with specific value in a custom field into my loop?
- Order by multiple meta keys on wordpress
- wp_query pagination links producing 404
- Display First posts without the default featured image
- Pagination problem with multiple loops on the same page
- Can’t seem to get an else statement correct? [closed]
- Output ACF field dynamicaly within a taxonomy loop [closed]
- How do I split a large query with a semi-expensive function included into multiple smaller queries