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( ']]>', ']]>', $content );
} else {
// Here you can grab the content of the default post.
}