How can i list current author's categories?

All we need is an easy explanation of the problem, so here it is.

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.

<?php
    $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
    ");
    ?>
    <ul>
        <?php foreach($categories as $category) : ?>
        <li>
            <a href="<?php echo get_category_link( $category->ID ); ?>" rel="nofollow noreferrer noopener" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
        </li>
        <?php endforeach; ?>
    </ul> 

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

try something like this…

you need to first call global $post;

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply