经过几个小时的谷歌搜索,我想出了以下解决方案。也许一次对某人有帮助。
1) 查询帖子使用gutenberg youtube块的位置:
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'s\' => \'core-embed/youtube\',
\'posts_per_page\' => 1
);
$query = new WP_Query($args);
2)从帖子的youtube块中提取URL
$post_id = 117;
$post = get_post($post_id);
$blocks = parse_blocks( $post->post_content );
function findYoutubeBlock(array $blocks) {
return $blocks[\'blockName\'] == \'core-embed/youtube\';
}
if (has_block(\'core-embed/youtube\', $post_id)) {
$youtube_block = reset(array_filter($blocks, \'findYoutubeBlock\'));
$youtube_url = $youtube_block[\'attrs\'][\'url\'];
}