How i can get user tags list , i get list of all tags with this:

<?php $tags = get_tags(); ?>

<?php if (isset($tags)): ?>

        <?php foreach ($tags as $tag): ?>
            <li><?php echo $tag->name; ?></li>
        <?php endforeach ?>

<?php endif ?>

This return me list of all used tags on my wordpress page.

My question is: How can I get only the tags assigned to posts written by the currently logged user?


1 Answer 1


The currently logged in user

The \WP_User object/instance can be retrieved via:

$user = get_current_user();

There's a shortcut API wrapper to fetch the ID:

$user_id = get_current_user_id();

$wpdb and the WP "DBAL"

WP basically uses $wpdb as DBAL/Database Abstraction Layer. It got plenty of public methods and some higher level wrappers for convenient access. One is get_posts_by_author_sql():

var_dump( get_posts_by_author_sql( 'post', true, get_current_user_id() ) );

# Result:
string 'WHERE post_author = 1 AND post_type = 'post' AND (post_status = 'publish' OR post_status = 'private')' (length=101)

If you just want public posts, use the 4th argument and set it to true.

The next step would be to build a complete query:

$sql = "select {$wpdb->posts}.id from {$wpdb->posts} ".get_posts_by_author_sql( 'post', true, get_current_user_id() );

Now you can bolt this onto a $wpdb query. Don't forget to only conditionally turn on debug mode. Also don't add debug to a live site as statically cached sites may print your DB credentials to the public for quite some time.

/** @var \wpdb $wpdb */
global $wpdb;
if (
    defined( 'WP_DEBUG' ) and WP_DEBUG
    and defined( 'WP_DEBUG_DISPLAY' ) and WP_DEBUG_DISPLAY

$posts = $wpdb->get_results( $sql );
// Filter out only the post IDs and build an array from the result:
$posts = wp_list_pluck( $posts, 'id' );

Now we only need to fetch the tags. We then build a new array and collect the tags - ordered by post IDs. This might end up having duplicate tags. If you don't want that, you will have to implement a different sorting and fetching mechanism. It's not that hard, as you can easily pluck terms based on the object_id property which in fact just is the post ID.

$tags = [];
foreach ( $posts as $post_id )
    $tags[ $post_id ] = get_the_tags( $post_id );

var_dump( $tags );

Keep in mind that prior to PHP v5.4 you will need array() instead of the short array syntax [].

The result will look like the following and you get full term objects in return.

array (size=13)
  167 => 
    array (size=16)
      50 => 
          public 'term_id' => int 50
          public 'name' => string '8BIT' (length=4)
          public 'slug' => string '8bit' (length=4)
          public 'term_group' => int 0
          public 'term_taxonomy_id' => int 50
          public 'taxonomy' => string 'post_tag' (length=8)
          public 'description' => string 'Tags posts about 8BIT.' (length=22)
          public 'parent' => int 0
          public 'count' => int 1
          public 'object_id' => int 167
          public 'filter' => string 'raw' (length=3)
      51 => 
          public 'term_id' => int 51
          public 'name' => string 'Articles' (length=8)
          public 'slug' => string 'articles' (length=8)
          public 'term_group' => int 0

Posts amount

To fetch the amount of posts the currently logged in user has written, use:

$count = count_user_posts( get_current_user_id() );
// Will return an integer
  • @user3501587 You are welcome :)
    – kaiser
    Jan 27, 2015 at 11:02
  • How i can remove repeatable tags from list ? Thanks! Feb 3, 2015 at 11:50
  • That highly depends on what you want to do. What if a repeated tag is needed in list B, but not in A and vice versa? Anyway, you need to modify the foreach loop and just build a different array.
    – kaiser
    Feb 3, 2015 at 11:53
  • okey, thanks. and next question, how i can get "future" posts? what add to sql? Feb 3, 2015 at 12:07
  • Please, search the site or ask a new question for that. There is no such thing as 1:1 support in comments. Thanks.
    – kaiser
    Feb 3, 2015 at 12:15

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.