Your problem is that the variable passed to setup_postdata()
must be the global $post
variable, like this:
// Reference global $post variable.
global $post;
// Get posts.
$posts = get_posts(array(
'post_type' => 'post',
'post_count' => 4
));
// Set global post variable to first post.
$post = $posts[0];
// Setup post data.
setup_postdata( $post );
// Output template part.
get_template_part( 'template-parts/post-thumbnail' );
// Reset post data.
wp_reset_postdata();
Now normal template functions, like the_post_thumbnail()
inside the template part will reference the correct post.
Related Posts:
- WP_Query vs get_posts
- How-to exclude terms from the main query the most performant way?
- get_posts() seemingly ignoring post_type
- Get posts for last working week in WP_Query
- Conflict with get_posts and the_content
- Best choice for multiple loop in page?
- get_posts output always same post
- 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
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- Populate select list with meta values from all posts of a Custom Post Type
- Related posts queries
- Ordering Posts by parent category, name ascending
- Both WP_Query and get_posts returning 1 post
- tax_query not working properly with get_posts
- 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?
- Using get_posts vs. WP_Query
- Get array of posts from the current archive page loop
- WordPress Custom Query to show posts from last x years
- How to order posts tag by tag?
- Loop through all tags & output posts in alphabetical list
- Pagination with 5 posts per page
- Different Results with query(‘s=computer’) vs get_posts(‘s=computer’)?
- How to display 3 different loops in 3 columns on homepage
- WP_query category__in not working, only pulls from first category
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- WP_Query to get posts in a specific category and post format
- Can’t get drafts with WP_Query using post_status parameter
- How do you Query posts with nothing in common?
- WP_Query doesn’t get sticky post at the top, when used in a page template
- Prevent duplicate post from dynamic multiple loops
- if/else on custom query gives 200 OK when condition not met?
- Multiple Loops Inside a Function
- Sticky posts are not showing at the top of category posts
- Storing query variable and then using wp_reset_query
- Can I set my WP_Query to be a Main Query?
- Help with WP_Query – Too many mySQL queries
- Group and list posts by custom taxonomy
- Show scheduled posts in main loop but not in WP_Query?
- Change query based on post type while staying in loop
- Get a list of posts with associated meta_value
- $WP_Query: How to display excerpt on first post only
- How to make search for posts using get method?
- How to get list of posts from permalinks?
- Check return value of get_posts
- Query post with meta value in set of values
- get_template_part causes 500 error
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- WP_Query – Adding “offset” posts to the end of the loop
- Conditional operator OR not working with custom fields
- Get posts that do not have the same tags as current
- wp_query is showing posts from other categories
- Should I reset $wp_query?
- Exclude a Woocommerce product from WP_Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- how to get category`s slug in WP_Query loop?
- Posts query according to meta box date
- 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
- Get posts with no tags?
- get_posts query matches too many results
- wp-query, pull children of parent page
- I am officially missing something about transient posts
- Advise on Templates for Custom Queries
- How do I get the content of a custom instance of WP_Query?
- 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?
- Show a message if there are no active posts in category
- Avoid removing duplicate posts
- 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?
- Custom post type loop error: Trying to get property of non-object
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- Getting the last X posts, but in ascending order of time
- Display three sequential posts on each page load, without repeating previous
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- 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
- How do I display posts with specific value in a custom field into my loop?
- Order by multiple meta keys on wordpress
- Retrieve posts in custom post type and specific taxonomies
- 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
- How to replecate the _fields parameter in custom REST api endpoint
- WordPress extremely slow when using get_posts with multiple meta_query relations
- Pagination only showed when no category is set in wp_query
- Sort by multiple columns using get_posts