WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

hey hii everyone, i have a function to fetch the videos from the rss feed of youtube, so that i can display selected videos to my blog for that i am using this youtube-search-feed url http://gdata.youtube.com/feeds/base/videos?q=eminem&client=ytapi-youtube-search&v=2

in the url, eminem is included, so it will display me videos of eminem, now my problem is, instead of eminem it should take my posts title name, so if i write this code

$rss = fetch_rss('http://gdata.youtube.com/feeds/base/videos?q='.the_title().'&client=ytapi-youtube-search&v=2');

then its returning me with a false video

here is my entire code

$newvar = str_replace(' ', '', the_title());
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://gdata.youtube.com/feeds/base/videos?q='.$newvar.'&client=ytapi-youtube-search&v=2');
    $maxitems = 1; 
    $items = array_slice($rss->items, 0, $maxitems);

here i am using $newvar to strip the spaces in between of the title of the posts @rarst i change the code but now i am not getting the video

here is the code

$newvar = str_replace(' ', '', the_title());
include_once(ABSPATH . WPINC . '/feed.php');
$feed = fetch_feed('http://gdata.youtube.com/feeds/base/videos?q='.$newvar.'&client=ytapi-youtube-search&v=2');
$maxitems = 1; 
$items = array_slice($feed->items, 0, $maxitems);
share|improve this question
Could you please add actual title(s) example? Also fetch_rss() is very outdated and deprecated, please use fetch_feed(). – Rarst Jan 11 '11 at 13:07
I would use urlencode() around the title, so you never have problems with special characters in the title. This way you don't have to remove spaces. – Jan Fabry Jan 11 '11 at 13:28
i added the fetch_feed() but now i am getting no videos check my og answer for current edited code – ntechi Jan 11 '11 at 13:53
Using urlencode as @Jan Fabry pointed out will give you better results, but you should get search results either way. Can you show us the code that displays the results? Or try var_dump($items) to see what format the results are in? – goldenapples Jan 11 '11 at 16:59
up vote 1 down vote accepted

Don't know if this is your only issue, but you're using the_title() to build your URI. This ECHOS the title, which is not what you want.

Instead, you should be using get_the_title().

I'd also urlencode the title, as Jan suggested.

Your best bet is to print out the YouTube URI after you build it, then try to hit it in a browser. If the results aren't right, the problem is with your URI.

share|improve this answer
ok I used get_the_title() and it worked for me, but now the problem is when the title has two words then i have to remove spaces between them, but i tried str_replace and urlencode but it doesn't works, how can i remove spaces between the title? – ntechi Jan 12 '11 at 5:09
hey made it, now the videos are coming, but just last thing i am still using fetch_rss function will there be any problem with this in future? – ntechi Jan 12 '11 at 6:47
Yes--fetch_rss() is deprecated, meaning it may be removed from future versions of Wordpress. You should switch to using fetch_feed. But note that they have slightly different usage, and you'll need to update your code--you can't just change the function name. Read the docs on fetch_feed (as Rarst suggested) for more info. – MathSmath Jan 12 '11 at 15:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.