Skip to content

Instantly share code, notes, and snippets.

@LC43
Last active November 29, 2023 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LC43/8b4ea6dbe0fe59e8579c75f7646b9635 to your computer and use it in GitHub Desktop.
Save LC43/8b4ea6dbe0fe59e8579c75f7646b9635 to your computer and use it in GitHub Desktop.
show more columns: author, date and last edit: and and a new filter: Author:
<?php
/**
* *******
* Interactive Geo Maps user filter
* ********************************
*/
add_filter( 'manage_igmap_posts_columns', 'igmaps_cols', 11 );
function igmaps_cols( $columns ) {
$columns['author'] = 'Author';
$columns['date'] = 'Date';
$columns['last_edited'] = 'Last Edited';
return $columns;
}
// Display the content of the Last Edited column
function display_last_edited_column_content( $column, $post_id ) {
// Display the last edited date for the post
if ( $column == 'last_edited' ) {
$last_edited = get_the_modified_date( 'Y-m-d H:i:s', $post_id );
echo $last_edited;
}
}
add_action( 'manage_igmap_posts_custom_column', 'display_last_edited_column_content', 10, 2 );
// Make the Last Edited column sortable
function make_last_edited_column_sortable( $sortable_columns ) {
$sortable_columns['last_edited'] = 'last_edited';
return $sortable_columns;
}
add_filter( 'manage_edit-igmap_sortable_columns', 'make_last_edited_column_sortable' );
// Add the filter dropdown to the 'igmap' custom post type
function add_author_filter_to_igmap_list() {
global $typenow;
// Ensure we're on the 'igmap' custom post type
if ( $typenow === 'igmap' ) {
// Get all posts of the specified Custom Post Type
$args = array(
'post_type' => 'igmap',
'posts_per_page' => 500,
'fields' => 'all',
'suppress_filters' => true, // Ignore pre_get_posts hooks
);
$cpt_entries = new WP_Query( $args );
// If there are posts, retrieve the unique authors
$authors = [];
if ( $cpt_entries->have_posts() ) {
foreach ( $cpt_entries->posts as $post_entry ) {
$author_id = $post_entry->post_author;
if ( ! empty( $authors[ $author_id ] ) ) {
++$authors[ $author_id ]->igmaps_count;
continue;
}
$authors[ $author_id ] = get_userdata( $author_id );
$authors[ $author_id ]->igmaps_count = 1;
}
}
if ( ! empty( $authors ) ) {
echo '<select name="igmap_author_filter">';
echo '<option value="">Filter by Author</option>';
foreach ( $authors as $author ) {
$selected = isset( $_GET['igmap_author_filter'] ) && $_GET['igmap_author_filter'] == $author->ID ? 'selected' : '';
echo '<option value="' . $author->ID . '" ' . $selected . '>' . $author->display_name . ' ( ' . $author->data->igmaps_count . ' ) </option>';
}
echo '</select>';
}
}
}
add_action( 'restrict_manage_posts', 'add_author_filter_to_igmap_list', 999 );
// Modify the query based on the selected author filter
function filter_igmap_by_author( $query ) {
global $pagenow;
// Ensure we're on the 'edit.php' page and dealing with the 'igmap' custom post type
if ( is_admin() && $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'igmap' && isset( $_GET['igmap_author_filter'] ) ) {
$query->query_vars['author'] = $_GET['igmap_author_filter'];
}
}
add_filter( 'parse_query', 'filter_igmap_by_author' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment