对get_page_by_path()
成功后将返回一个post对象或数组,而不是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
所以没有必要定制
new WP_Query
打电话,您的以下代码正常:
$faq_post = get_page_by_path( $post_slug, OBJECT, \'land\' );
$content = $faq_post->post_content;
然而,如果我正确理解你
$post_slug
应为:
$post_slug = \'landing-faq-\' . $post->post_name;
检查一下
$faq_post
不是
null
(表示故障):
$content = $faq_post ? $faq_post->post_content : \'\';
执行自定义SQL查询来搜索带有该段代码的帖子可能会更快:
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.
}