• Resolved bigsmoke

    (@bigsmoke)


    Hi,

    I’m looking for a quick and easy way to get a list of posts that have (YouTube) URL’s embedded, and from that extract a list of those embeds,

    The purpose of this is to be able to show the visitor the “latest video” on the homepage of a WordPress site that I’m working on and to create a simple gallery page with thumbnails linking to all those videos.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Do a regular search and use youtube.com as the search term, seeing as the search function looks at post content and titles it will find all the posts that have a youtube URL in them.

    Search defaults to order by date, so the first result will be the newest post with an embed.

    If your youtube videos are always inserted using the URL you could be a little more specific and use youtube.com/watch?v= as the search term.

    Example code for use in a template file (barebones example).

    <?php
    // Args
    $args = array(
    	's' => 'youtube.com/watch?v=',
    	'posts_per_page' => 1
    );
    
    // Setup query
    query_posts( $args );
    
    // Check query has results
    if( have_posts() ) :
    
    	// Do the loopy loop
    	while( have_posts() ) :
    		the_post();
    		the_title();
    	endwhile;
    
    endif;
    ?>

    Thread Starter bigsmoke

    (@bigsmoke)

    Thanks for guidance!

    I use the following code to get the latest video for my homepage:

    function aihato_latest_video() {
      $query_args = array(
        's' => 'youtube.com/watch?v=',
        'posts_per_page' => 1
      );
    
      $posts = get_posts( $query_args );
      $post1 = $posts[0];
    
      $matches = array();
    
      preg_match('|http://www.youtube.com/watch\?v=([a-zA-Z0-9]+)|', $post1->post_content, $matches);
      $v = $matches[1];
    ?>
    <object width="415" height="250">
      <param name="movie" value="http://www.youtube.com/v/<?php echo $v ?>&hl=en_US&fs=1&"></param>
      <param name="allowFullScreen" value="true"></param>
      <param name="allowscriptaccess" value="always"></param>
      <embed src="http://www.youtube.com/v/<?php echo $v ?>&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="250"></embed>
    </object>
    <?php
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Extracting all YouTube embeds from all posts’ is closed to new replies.