0

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;
$post_slug=$post->post_name;

$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; ?>
        </div>
        <?php
    endwhile;
endif;

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.

1

Yes, get_page_by_path() will return a post object or array on success, and not the post ID.

$faq_post = get_page_by_path( $post_slug, OBJECT, 'land' );  // object
$faq_post = get_page_by_path( $post_slug, ARRAY_A, 'land' ); // array

So there is no need to make the custom new WP_Query call, and your code below is good:

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

However, if I understand you properly, the $post_slug should be:

$post_slug = 'landing-faq-' . $post->post_name;

And check that $faq_post is not a null (which indicates a failure):

$content = $faq_post ? $faq_post->post_content : '';

And it might be faster to perform a custom SQL query to search for the post with that slug:

global $wpdb;

$row = $wpdb->get_row( $wpdb->prepare( "
    SELECT ID, post_content FROM {$wpdb->posts}
    WHERE post_name = %s AND post_type = 'land'
", $post_slug ) );

if ( $row ) {
    $content = $row->post_content;

    // If you need to apply all filters to the content.
    $content = apply_filters( 'the_content', $content );
    $content = str_replace( ']]>', ']]&gt;', $content );
} else {
    // Here you can grab the content of the default post.
}
  • ok great. good to know I was headed down the right path. I'm sure the custom query is faster. so I'll be using that instead. Now, does all this belong directly in my template file or do I need to put it in functions.php and somehow call it from the template? – Daveh0 Nov 9 '18 at 12:58
  • Yes, you can use it in place of the lines starting from the $faq_id to endif; in your code (the first snippet in the question). Or you can replace that with this.. – Sally CJ Nov 9 '18 at 14:34
  • Awesome - I get it now. Thanks for the help/guidance! – Daveh0 Nov 9 '18 at 15:26
  • As always, I'm glad I could help. And you're welcome. :) – Sally CJ Nov 9 '18 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.