The Wrong Way to Include WordPress Pages
If you are looking for a function like show_post("pageA") to embed the content of one page directly into another, that specific utility does not exist in the core API. This expectation often stems from legacy web development practices where server-side includes or simple file injections were common. In modern WordPress, however, pages are distinct database entities with their own post meta, revision history, and rendering contexts. You cannot simply "load" a page's HTML string as if it were a raw text file without considering the separation of concerns that the platform enforces.
The core issue is understanding how WordPress renders content. When you visit a URL for pageA, WordPress runs the main loop, applies the active theme's template hierarchy, and processes dynamic elements like shortcodes or widgets before outputting the HTML. If your goal is to duplicate that exact rendered output on pageX without creating new posts, the most straightforward approach involves retrieving the post object programmatically and rendering its content manually within the current request context.
To achieve this, you need to access the global $wp_query or directly query the database using WP_Query. However, simply fetching the content via get_the_content() is rarely sufficient for a true "include" operation. That function returns the raw post content but skips the template processing that happens on the frontend. If your pages rely on theme-specific markup (like headers within the page loop) or dynamic data that only loads when the page is requested, a direct output of get_the_content() will miss those elements.
A more robust solution involves creating a custom shortcode or using a hook to inject the content during the rendering phase. If you are building a specific dashboard or admin view where this behavior is required, you can use WP_Query to fetch the target pages by their slug and then manually loop through them to output their content. Here is a conceptual example of how that logic might look in a plugin or theme file:
function include_page_content_by_slug( $slug ) {
global $post;
$args = array(
'post_type' => 'page',
'posts_per_page' => 1,
'name' => $slug // Note: Using 'name' expects the slug to match exactly
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Check if we want the raw content or the full loop output
if ( is_front_page() || ! is_singular() ) {
// For a true "include" that mimics the page template:
// You typically need to set up the global context manually.
echo get_the_content();
}
}
}
wp_reset_postdata();
}
Using get_the_content() here returns the raw data stored in the database. It does not execute the template logic associated with that specific page type unless you are actually visiting that URL. If your pages utilize custom post types or complex template hierarchy, this method might result in missing styles or script registrations that occur only when that specific page is active.
A critical pitfall to avoid is creating a circular dependency or performance bottleneck if you attempt to render entire loops for multiple pages simultaneously. Loading full posts can trigger database queries for meta data and relations that are unnecessary if you only need the text content. In cases where you require the exact rendered HTML including theme-specific wrappers, you might need to temporarily switch to the target page's template context or accept the limitations of static inclusion versus dynamic rendering.
If your use case involves displaying multiple pages on a single landing page (like pageX aggregating pageA, pageB, and pageC), consider whether a custom post type or a taxonomy-based approach would be more scalable than hardcoding slugs into your logic. This ensures that content management remains decoupled from the display logic, adhering to WordPress best practices for maintainability.
For developers transitioning from other frameworks or seeking similar functionality in different stacks, Laravel offers a cleaner syntax for such tasks via Blade directives like @include, which handle the underlying file inclusion differently. However, within the WordPress ecosystem, sticking to standard querying and rendering methods ensures compatibility with caching layers and theme customization hooks.