Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Last active September 13, 2017 14:28
Show Gist options
  • Save 2ndkauboy/ef9d7e602851b4236c404fe8f35f21ce to your computer and use it in GitHub Desktop.
Save 2ndkauboy/ef9d7e602851b4236c404fe8f35f21ce to your computer and use it in GitHub Desktop.
<?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