Your function works in your page template but not in the sidebar because at the point that your template is processed, $post already contains the post that has been loaded for the page.
I tried your code and, just like Michael said, all I needed to add was the global declaration of $post inside the function, and it displayed the posts exactly as you intended:
function myRecentPosts($postType){
wp_reset_postdata();
$args = array( 'post_type' => $postType,'posts_per_page' => 3);
global $post;
$recentPosts = get_posts( $args );
foreach($recentPosts as $post){
setup_postdata($post); ?>
<article>
<h1><?php the_title();?></h1>
<?php the_excerpt();?>
</article>
<?php
}
wp_reset_postdata();
}
Related Posts:
- Only get_posts of certain post formats
- Is the first item returned by get_posts() always the latest post?
- How to retrieve a value from get_posts()? [closed]
- Getting movie and serial on actor page
- how to display active, upcoming and past event with featured listing with pagination
- Display 4 chronological posts starting with a random post
- How do I output a database option that is an array into a get_posts array?
- Advanced Custom Fields: Post Object – Not returning data [closed]
- Why not yield/iterable in posts interface?
- Logged in user ID as post ID
- How do I exclude recent post from recent post php widget
- Open post-content in archive page in a Modal box with bootstrap
- get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
- Query if audio attachment AND/OR custom field
- How do I get post attachments in menu_order in WordPress?
- PHP Syntax Error on get_posts
- How i can get post data from database when i am getting post id from url in wordpress
- How to retrive the text : No movie by this actor
- How to get image from url from the database?
- Unable to render custom field after attempt to generate a list of recent post in page template
- Add class to every other posts using get_posts
- search.php to search only the post title
- How to get woocommerce orders with get_posts method in a shortcode
- get value from get_post_meta then reuse it in another get_post_meta
- Foreach Loop Of Post Types With Nested If Else If Not Completing Process
- How can I render more than multiple posts on template-parts with post formats
- How to display post_content from database in different on template page?
- Displaying POST content with HTML tags and all
- Create a hierarchical loop at predefined markup requirements
- offset and max_num_pages in pagination gallery
- Even with PHP plugin get_posts not working in widget area
- Sort posts by title, sort array by largest number [duplicate]
- Transient Loop Not working as expected
- show/hide attachments
- Find owner of coupon
- Link on post title only if post have content
- Category Attachment Pagination 404 Error on 2nd Page
- Custom plugin: Trying to show saved data on frontend
- Is there a browser plugin or method to find which php template an item is coming from?
- Multisite get_home_url(); – Getting URL for current site
- Display posts by month
- Using file_exists to check file in Uploads
- Retrieving and Displaying Data From a Table
- On update to 3.04 site started recording accents as “á” and “á”, what to do?
- Use composer to load custom classes [closed]
- Use two WP Query in template
- apache cpu over 70% on localhost
- How can I use wp_insert_comment to write a comment when a post is edited?
- How can I use a modal window to display the current post in loops featured image?
- How to query for pages/post depending on slug?
- How do you add customer capability after Woocommerce purchase?
- How to make a search button that will search my website?
- Only echo item in foreach with highest value [closed]
- get_the_tags() not iterating through for/while loop, but will with foreach
- Is there an error in my code?
- How to disable server signature on wordpress? [closed]
- wp attempt focus, disabling without touching core
- Shortcode to show the code
- db converts decimal number wrong
- How do I find PHP file that contains content of my page?
- How to keep empty fields from appearing in php?
- Include Parent Term in wp_list_categories
- is_user_logged_in() not working in AJAX validation call
- Does anyone know all parameters of get_comments()
- preg_replace regex problem
- malware undetectable by multiple scans
- WordPress child pages not working
- PHP Output also in Child theme, but different
- Check in which custom taxonomy belongs and change the style
- How do I add my PHP app to a WordPress page whilst keeping semantic URLs?
- Query the links Database
- How to internationalize header image?
- WordPress cascading dropdown selection search based on Parent Page & Child Page
- Settings API form – submit with AJAX
- WordPress 5.6 Application Passwords
- Get title of page containing post grid within the posts
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- WordPress creating a compund query or using data from one post type to access another
- Store data from JavaScript object to custom table in user account
- Permalink doesn’t navigate to post (changes only url)
- Javascript Tab Gallery with Advanced Custom Fields
- Generate a radius search of custom post type locations
- custom field – changing an element or background of id div – different versions not working
- Is there a reason why my wordpress PHP page isn’t loading into my PHP template
- truncating imported rss-feeds
- WordPress 404 when using AJAX to call php function
- WordPress Setting API – There is no field in options.php
- WP_Query count of different meta key values [duplicate]
- How I can shorten this title length?
- Snapshot Theme from WooThemes — empty ‘alt’ attribute
- Converting Menu Links to List Elements
- I am trying to replace a string with other from function.php
- PHP warning: Undefined array key 2 in feed.php
- How to continuously developing a WP site that is already deployed in production without damaging it during development
- Custom field fetch error on woocommerce pdf invoice
- posts_clauses drop ACF get_field function
- WordPress @include( ‘template-config.php’ );
- Is there a hook that I can use when a fatal error occurs?
- in_footer: gives syntax error
- Embed dynamic media query in a Gutenberg block