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

My Code getting authors written post categories but just 2 ID has author lists category.. I replace $current_user->ID but it is not worked.

    $categories = $wpdb->get_results("
        SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
        FROM $wpdb->posts as posts
        LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
        LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
        LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
        WHERE 1=1 AND (
            posts.post_status = 'publish' AND
            posts.post_author = '2' AND
            tax.taxonomy = 'category' )
        ORDER BY terms.name ASC
        <?php foreach($categories as $category) : ?>
            <a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
        <?php endforeach; ?>
share|improve this question
up vote 1 down vote accepted

try something like this...

you need to first call global $post;

then get the author id like $author_id = $post->post_author;

share|improve this answer
Worked! Thanks @Reigel – Genxer Jan 15 '13 at 3:20
I have a question again. I used this code in sidebar.php its worked perfectly but when i use thats code with sidebar php widget.( My plugin 'exec-php' ) i see error : 'Fatal error: Call to a member function get_results() on a non-object in website.com\wp-content\plugins\exec-php\includes\runtime.php(42) : eval()'d code on line 3' and runtime.php 42 line have this code: eval("?>$content<?php "); How can i use it in my php widget. I changed code a little it here – Genxer Jan 15 '13 at 19:15
try adding global $wpdb; – Reigel Jan 16 '13 at 0:35
that would then be global $post, $wpdb; – Reigel Jan 16 '13 at 0:36
Yes it's worked thanka – Genxer Jan 16 '13 at 13:00

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.