$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WP Query by Gutenberg block and get its attribute|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WP Query by Gutenberg block and get its attribute

matteradmin11PV0评论

I need to fetch on the front page youtube video from the latest post where was used gutenberg youtube block.

I guess I need:

1) query such posts

2) extract the youtube URL from the block

Is there a way how to do that?

I need to fetch on the front page youtube video from the latest post where was used gutenberg youtube block.

I guess I need:

1) query such posts

2) extract the youtube URL from the block

Is there a way how to do that?

Share Improve this question edited Mar 2, 2019 at 19:20 Tomáš Vavřinka asked Mar 2, 2019 at 14:30 Tomáš VavřinkaTomáš Vavřinka 1979 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

After hours of googling I came up with following solution. Maybe once will help to someone.

1) query post where is used gutenberg youtube block:

$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    's'                 => 'core-embed/youtube',
    'posts_per_page'    => 1
);

$query = new WP_Query($args);

2) extract the URL from youtube block of the post

$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'];
}
Post a comment

comment list (0)

  1. No comments so far