Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/**
* Private Posts in Tag Cloud
*
* @package PrivatePostsinTagCloud
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: Private posts in tag cloud
* Plugin URI: https://kau-boys.de
* Description: Show tags in the tag cloud for posts that are private, when the user is logged in.
* Version: 0.1
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Show tags in the tag cloud for posts that are private, when the user is logged in.
*
* @param array $pieces Terms query SQL clauses.
* @param array $taxonomies An array of taxonomies.
* @param array $args An array of terms query arguments.
*
* @return array
*/
function private_posts_in_tag_cloud_terms_clauses( $pieces, $taxonomies, $args ) {
if ( isset( $args['largest'] ) && is_user_logged_in() ) {
// Count by the grouped term_id.
$pieces['fields'] .= ', COUNT(t.term_id) AS grouped_count';
// Join the relationship to the posts.
$pieces['join'] .= ' INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id';
// Remove the "redundant" count (only for public posts).
$pieces['where'] = str_replace( 'AND tt.count > 0', '', $pieces['where'] );
// Group by the term_id to remove duplicates and calculate the count.
$pieces['where'] .= ' GROUP BY tt.term_id HAVING grouped_count > 0';
}
return $pieces;
}
add_filter( 'terms_clauses', 'private_posts_in_tag_cloud_terms_clauses', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment