inserting content of 1 Post to in another with a template hierarchy

I have a page template I use for landing pages for various ad campaigns. The template uses custom fields for things like headlines and other shorter pieces of content within the page. However the template includes an entire section of FAQs. In many cases, the FAQs are the same across landing pages, but the requirement arose to have the ability to use a different set of FAQs (or different, structured/formatted content altogether) in that space if needed. I have built some logic into the template that will fetch the_content of a post containing a default set of FAQs and add it on to the_content of the landing page itself. Within this, I’m trying to add some additional logic that works similar to WP’s template hierarchy… ‘if you find a post named this use it, if not look for a post named that and use IT and if you don’t find either, use this default’.

I’m basing it off of the slug of the landing page. So if the landing page’s slug is holiday1 and there’s a post with a slug of landing-faq-holiay1, it will be inserted into the landing page. And if not, the default post with a slug of landing-faq will be inserted.

I’m using the following code to insert the_content:

global $post;

$faq_id = get_page_by_path( $post_slug, OBJECT, ‘land’ )->ID;

$query = new WP_query( array(‘page_id’ => $faq_id) );

if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$content = apply_filters(‘the_content’, get_the_content());
<div class=”content”>
<?php echo $content; ?>

This all works well and good (for the sake of simplicity, I left out the conditional logic to fall back on the default FAQ post), but I’ve come to realize that calling get_page_by_path() returns an entire Post object… including post_content.

In this case, rather than running a separate WP_query and looping through could I just do something like:

$faq_post = get_page_by_path( $post_slug, OBJECT, ‘land’ );
$content = $faq_post->post_content;


I know get_template_part() has this built in, but the the “partial” in this case must be a Post that the client can edit like the rest of their content using a visual page builder plugin.

Read more here:: inserting content of 1 Post to in another with a template hierarchy

Leave a Reply

Your email address will not be published. Required fields are marked *