Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have used this code in header.php

### get the author info
global $current_user;
get_currentuserinfo();
$authorid = $current_user->ID;
### get the last post id using author id
$info=mysql_query("SELECT * FROM wp_posts WHERE post_author = $authorid ORDER BY post_date DESC LIMIT 1");
$array_content = mysql_fetch_array( $info );
$postid_profile = $array_content['ID'];

When i print the $postid_profile in header.php it displays correctly. But if i print the $postid_profile in single.php it displays nothing. I couldn't track the error. How can i fix this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You call your header.php file by calling get_header() function in single.php file. It means that your $postid_profile variable exists only in scope of get_header() function. To make it visible in global scope you need to make your variable global.

So your code should be modified like this:

### get the author info
global $current_user, $postid_profile;
get_currentuserinfo();
$authorid = $current_user->ID;
### get the last post id using author id
$info=mysql_query("SELECT * FROM wp_posts WHERE post_author = $authorid ORDER BY post_date DESC LIMIT 1");
$array_content = mysql_fetch_array( $info );
$postid_profile = $array_content['ID'];

Now your $postid_profile variable will exist in global scope and will be accessible in single.php file.

As additional I would strongly recommend you to use $wpdb variable to perform all db queries calls. Plus to get current user id you can use get_current_user_id() function. So your code should be like this:

### get the author info
global $postid_profile;
get_currentuserinfo();
$authorid = get_current_user_id();
### get the last post id using author id
$array_content = $wpdb->get_row("SELECT * FROM wp_posts WHERE post_author = $authorid ORDER BY post_date DESC LIMIT 1", ARRAY_A);
$postid_profile = $array_content['ID'];
share|improve this answer
    
It works perfect. Thanks for your detailed answer. –  designersvsoft Jun 1 '12 at 7:16

Your Answer

 
discard

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.