Some post-related data is not available to get_posts by default, such as post content through the_content()
, or the numeric ID. This is resolved by calling an internal function setup_postdata()
, with the $post array as its argument:
<?php
$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<h2><a href="https://wordpress.stackexchange.com/questions/28905/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata();
?>
Related Posts:
- Get random custom-post-type post to display within content
- Getting a div content of a post in wordpress?
- Sanitizing post content for use in an email
- setup_postdata is not returning any values
- Wp-query causing problems with the_content();
- How do I set the content for a post of a particular CPT?
- Get permalink and title by post name?
- Rendering Custom Post Types independently of the theme in use
- Add $more_link_text parameter to the_excerpt()
- Exclude the_content (); from page password protection
- the_content() not outputting anything (Advanced Custom Fields)
- Content vs Template on custom post
- the_content filter on some post types only not working
- Adding the_content() in custom template email
- Is it better to use filter for the_content in archive pages?
- Multiple templates for single custom post type
- help
- the_content(); of custom post type treated as an array or object
- Dynamic bootstrap tabs with post_title doesn’t display the_content
- How can I remove filters from custom post types?
- Add shortcode inside of the_content()
- Separate Content from gallery (custom post type)
- Getting custom post type author and date published
- filter the_content, custom post type, and wp_query
- Add Content Column to Custom Post Type backend
- Creating post template for a Custom Post Type
- Auto generate excerpt from ACF field on a CPT that does not support excerpt or content
- Filter Content on all Post Types
- Expandable WordPress content (text) inside a post
- Calling the first & last post by category in custom post type
- Tags results not displaying for post types
- Add HTML before a specific div?
- How to change permalink structure for custom post type and it’s taxonomies?
- No Edit / Delete Links for Custom Post Type?
- custom post types don’t appear in RSS
- Template tags to display custom post type posts in category template?
- Change 2 fields in the post depending on the 3rd field before save
- How does one flush rules on theme activation or deactivation only?
- Front end post form won’t post categories to custom post type
- Update post date to modified date automatically
- How do I create a page template to display a custom post type?
- Custom search with Custom Fields in WP REST API?
- Developing WordPress on Kubernetes
- After adding add_role to functions.php and creating a user, can not login into admin
- Ordering posts in Search & taxonomy by post_meta
- Custom post type is showing custom home page, not archive page
- WordPress Custom Post Type Permalinks dynamic term slugs
- WordPress comments_open for specific user role or post type
- Custom Post Slug same as Parents Category Slug
- How to define which register_post_status goes to which register_post_type?
- Assign Custom post to Custom Taxonomy
- Show post with thumbnails only
- How to add custom taxonomy in custom post type permalink?
- Delete “Post Published. View Post” for custom post type
- Create an archive page for custom post type with custom taxonomy
- Query custom post type based on post id and custom field value
- Changing slug of all posts
- Share root slug for Child Page and Custom Post Type (prioritizing child pages over posts)
- Pull in taxonomy field of custom post type in new query
- Use the custom post type archive for taxonomies?
- Apply template by path/slug related to custom post type?
- WP Query results showing posts outside of category ID
- How to count other posts not having specific taxonomy terms?
- How do I disply an array inside a custom post type?
- Is there a way to group specific CPTs under a single dashboard menu item?
- Order users by number of posts (includes custom post type)
- Under What Conditions Can I name my CPT the same as my CPT Page?
- custom post type capabilities – needs to be VERY secure
- Metabox does not show in custom post type
- Need help with Custom Post Types and Taxonomies
- How can I tell if I’m on a custom post type archive page?
- Can’t Get Parent and Child Categories of Custom Taxonomy to Display
- Query all posts under one taxonomy? Why is my code not working?
- Custom rewrite rule takes place AFTER the generic category rule
- How to Enable Ascending or Descending In WordPress Default Built-in Loop
- See if the email exists or not
- Search CPT by tag ids
- WP_Query not using custom taxonomy categories on custom post type
- How would i insert a value of custom field from Advaced Custom Field into shortcode generated by Gravity Forms [closed]
- Get Previous Post based on Custom Field Name, for Custom Post Type
- Imported posts for cpt are imported as scheduled, not published [SOLVED]
- Add specific route for Custom Post Type (add/edit…)
- Custom Field to post_title
- Stripe multiple transactions
- Custom post type with two templates
- Post Custom & Taxonomy 404 error
- Custom Post Type – Admin Columns
- Query shopp categories and CPT
- previous_posts_link returns null when querying custom post type
- How to scroll through post thumbnails?
- WordPress can only query up to 766
- CPT Archive with parent page
- Adding rewrite rules to Custom Post-type mixed with Taxonomy
- fetch meta fields inside a widget
- Inserting two categories
- Use a Variable in update_post_meta as the $meta_key
- How to manage a bookstore
- How to set up a echo? [closed]
- Change post structure
- selecting custom post types and taxonomies for hub page listing blocks