Skip to content

Instantly share code, notes, and snippets.

@wplit
Created May 12, 2025 12:28
Show Gist options
  • Save wplit/5bd0bba081ddaa8d43cb2dce54594c99 to your computer and use it in GitHub Desktop.
Save wplit/5bd0bba081ddaa8d43cb2dce54594c99 to your computer and use it in GitHub Desktop.
add favorites column to posts screen
<?php
// Add a custom column to display favorite count of each post
add_filter('manage_post_posts_columns', 'add_favorited_users_column');
function add_favorited_users_column($columns) {
$columns['favorited_users'] = 'Favorited Count';
return $columns;
}
// Display the count in our custom column
add_action('manage_post_posts_custom_column', 'show_favorited_users_column_content', 10, 2);
function show_favorited_users_column_content($column, $post_id) {
if ($column == 'favorited_users') {
echo bricksextras_post_favorite_count( $post_id );
}
}
// Optional / make column sortable
add_filter('manage_edit-post_sortable_columns', 'register_favorited_users_sortable_column');
function register_favorited_users_sortable_column($sortable_columns) {
$sortable_columns['favorited_users'] = 'favorited_users';
return $sortable_columns;
}
// Modify query to sort by the "Favorited Count" column
add_action('pre_get_posts', 'sort_by_favorited_users_column');
function sort_by_favorited_users_column($query) {
if (!is_admin() || !$query->is_main_query()) {
return;
}
// Check if we're sorting by the "Favorited Count" column
if ($query->get('orderby') === 'favorited_users') {
// Get the current order direction
$order = strtoupper($query->get('order')) === 'ASC' ? 'ASC' : 'DESC';
// Set a meta query that doesn't filter out posts
$query->set('meta_query', array(
'favorite_count_clause' => array(
'key' => 'x_favorite_users_count',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
),
'relation' => 'OR',
array(
'key' => 'x_favorite_users_count',
'compare' => 'NOT EXISTS',
),
));
// Order by our named clause
$query->set('orderby', array(
'favorite_count_clause' => $order,
));
// Remove the meta_key parameter as we're using the meta_query instead
$query->set('meta_key', '');
}
}
/**
* Add a meta box to display users who have favorited the post
*/
function add_favorite_users_meta_box() {
// Add the meta box to posts
add_meta_box(
'favorite_users_meta_box', // ID
'Users Who Favorited This Post', // Title
'display_favorite_users_meta_box', // Callback function
'post', // Screen (post type)
'side', // Context (side, normal, advanced)
'default' // Priority
);
// You can add to other post types as needed
// add_meta_box('favorite_users_meta_box', 'Users Who Favorited This Post', 'display_favorite_users_meta_box', 'page', 'side', 'default');
}
add_action('add_meta_boxes', 'add_favorite_users_meta_box');
/**
* Display the content of the meta box
*/
function display_favorite_users_meta_box($post) {
// Get users who favorited this post
$user_ids = bricksextras_post_favorite($post->ID);
// Display the count
echo '<p><strong>Total: ' . count($user_ids) . ' users</strong></p>';
// If no users have favorited this post
if (empty($user_ids)) {
echo '<p>No users have favorited this post yet.</p>';
return;
}
// Start the user list
echo '<ul class="favorite-users-list">';
// Loop through each user ID
foreach ($user_ids as $user_id) {
$user = get_userdata($user_id);
// Skip if user doesn't exist
if (!$user) {
continue;
}
// Display user info
echo '<li>';
echo '<a href="' . esc_url(get_edit_user_link($user_id)) . '">';
echo esc_html($user->display_name) . ' (' . esc_html($user->user_login) . ')';
echo '</a>';
echo '</li>';
}
echo '</ul>';
// Add some basic styling
echo '<style>
.favorite-users-list {
max-height: 200px;
overflow-y: auto;
margin: 0;
padding: 0 0 0 20px;
}
.favorite-users-list li {
margin-bottom: 5px;
}
</style>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment