Get tags for current user

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
)
    $wpdb->show_errors();

$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 => 
        object(stdClass)[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 => 
        object(stdClass)[10]
          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