i asked this question previously, which solved my problem, but further i am having still problem with it,

i have a function to display tweets of a user which fetch from a custom field, the custom field code is working perfect, cause when i echo the get_post_meta in my single.php i get the value which is input

now the question is when i try to fetch the value from custom field to a twitter function the tweets are not coming,

here is my code:

function tweet(){

$doc = new DOMDocument();

if ( $meta =get_post_meta($post->ID, 'dbt_Username', true) ) { 
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
  $outer = "<ul>";
  $max_tweets = 5;    

  $i = 1;
  foreach ($doc->getElementsByTagName('item') as $node) {
    $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

    $tweet = substr($tweet, stripos($tweet, ':') + 1);   

    $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
          '<a href="$1">$1</a>', $tweet);

    $tweet = preg_replace("/@([0-9a-zA-Z]+)/", 
          "<a href=\"http://twitter.com/$1\">@$1</a>", 

    $outer .= "Twiter Updates by:".get_option('tweetID')."<li>". $tweet  . "</li>\n";

    if($i++ >= $max_tweets) break;
   $outer .= "</ul>\n";
  return $outer;

add_action('admin_menu', 'tweet_fetch1');

function append_the_content1($content) {
     $content .= "<div class='post'><p>".tweet(get_post_meta($post->ID, 'dbt_Username', true))."</p></div>";
      return $content;
add_filter('the_content', 'append_the_content1');
  • What happens when you put <?php echo tweet(get_post_meta($post->ID, 'dbt_Username', true)); ?> directly into your single.php file? Does it work in that case? It seems to me that if it doesn't show in that case then it's a problem with that function. If it works, then it's a problem with the other function or the way you are calling it.
    – matt
    Dec 18, 2010 at 6:04
  • if i am just writiing <?php echo tweet(get_post_meta($post->ID, 'dbt_Username', true)); ?> then no div is coming and no tweets, if also i write only <?php tweet(); ?> then also nothin comes except the post i also think that the problem is with the function Dec 18, 2010 at 6:39

1 Answer 1


So many issues here...

First, I'm not sure what this line is doing:

add_action('admin_menu', 'tweet_fetch1');

If you don't actually have a function called tweet_fetch1 that runs when the admin loads, you should remove this add_action. And if you do have such a function, why is your add_action in this location? The first step to avoiding problems is clean, readable, commented code the groups functions and hooks them logically.

Second, in both of your functions, you're trying to reference $post->ID where no post object is available. You can usually make it available by globalizing it.

global $post;

Third, your function called "tweet" accepts no arguments, yet when you call it from inside your append_the_content1() function, you're trying to pass it the meta value for dbt_Username.

$content .= "<div class='post'><p>".tweet(get_post_meta($post->ID, 'dbt_Username', true))."</p></div>";

This is incorrect...and unnecessary, as you then grab the same meta value (again) from inside the tweet() function using get_post_meta. You need to remove the argument (the stuff between the parentheses) when you call the tweet() funciton. Like so:

$content .= "<div class='post'><p>".tweet()."</p></div>";

Lastly--though this is just a suggestion, and isn't strictly something wrong with your code--I don't know what all that preg replacing is for. If you're trying to link to the original tweet, you can pull the URI right out of the twitter feed. Also, you can make links inside tweets clickable with WordPress' handy make_clickable() function.

Your problem here isn't with get_post_meta--it's with PHP. No disrespect, but it would help you out tremendously to brush up on the basics so that you can better understand the code you're writing, and the answers people are giving you.

  • ok i understand this, first add_action('admin_menu', 'tweet_fetch1'); this line is of another function which by mistakingly i copied, global $post i ll try it now, and i didnt understand your third step, what arguments to pass to function, the custom fields which i created is also through a function, i wrote a function on it to display it in edit post page, from there i am getting the post_meta, so what changes i have to do in third step Dec 18, 2010 at 7:31
  • hey second and the happening thing, after putting global $post; it worked the tweets are coming, thanks a lot mate for your help Dec 18, 2010 at 7:34
  • 2
    Again, this is about you not understanding php. If you're defining a function that accepts an argument you do it like: function my_function($my_argument){//function code}. Your tweet function, however, is defined like: function tweet(){}, meaning it accepts no args. So when you call it, you can only call it like: tweet(), not like: tweet($something). I'm updating my answer to make this more clear.
    – MathSmath
    Dec 18, 2010 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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