Skip to content

Instantly share code, notes, and snippets.

@LC43
Last active November 29, 2023 14:06
Show Gist options
  • Select an option

  • Save LC43/8b4ea6dbe0fe59e8579c75f7646b9635 to your computer and use it in GitHub Desktop.

Select an option

Save LC43/8b4ea6dbe0fe59e8579c75f7646b9635 to your computer and use it in GitHub Desktop.

Revisions

  1. LC43 revised this gist Nov 29, 2023. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php

    /**
    * *******
    * Interactive Geo Maps user filter
    @@ -39,23 +40,22 @@ function add_author_filter_to_igmap_list() {

    // Ensure we're on the 'igmap' custom post type
    if ( $typenow === 'igmap' ) {
    // Customize the CPT slug
    $cpt_slug = 'igmap';

    // Get all posts of the specified Custom Post Type
    $args = array(
    'post_type' => $cpt_slug,
    'posts_per_page' => -1,
    'fields' => 'ids', // Only retrieve post IDs for efficiency
    '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() ) {
    $authors = array();
    foreach ( $cpt_entries->posts as $post_id ) {
    $author_id = get_post_field( 'post_author', $post_id );
    foreach ( $cpt_entries->posts as $post_entry ) {
    $author_id = $post_entry->post_author;
    if ( ! empty( $authors[ $author_id ] ) ) {
    ++$authors[ $author_id ]->igmaps_count;
    continue;
    @@ -80,7 +80,7 @@ function add_author_filter_to_igmap_list() {
    }
    }
    }
    add_action( 'restrict_manage_posts', 'add_author_filter_to_igmap_list' );
    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 ) {
  2. LC43 revised this gist Nov 29, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -43,11 +43,12 @@ function add_author_filter_to_igmap_list() {
    $cpt_slug = 'igmap';

    // Get all posts of the specified Custom Post Type
    $args = array(
    $args = array(
    'post_type' => $cpt_slug,
    'posts_per_page' => -1,
    'fields' => 'ids', // Only retrieve post IDs for efficiency
    );

    $cpt_entries = new WP_Query( $args );

    // If there are posts, retrieve the unique authors
  3. LC43 revised this gist Nov 29, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@

    <?php
    /**
    * *******
    * Interactive Geo Maps user filter
  4. LC43 revised this gist Nov 29, 2023. 1 changed file with 66 additions and 34 deletions.
    100 changes: 66 additions & 34 deletions admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -1,62 +1,94 @@
    <?php
    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 11 );

    /**
    * *******
    * 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['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) {
    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);
    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);
    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;
    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_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;
    global $typenow;

    // Ensure we're on the 'igmap' custom post type
    if ( $typenow === 'igmap' ) {
    // Customize the CPT slug
    $cpt_slug = 'igmap';

    // Get all posts of the specified Custom Post Type
    $args = array(
    'post_type' => $cpt_slug,
    'posts_per_page' => -1,
    'fields' => 'ids', // Only retrieve post IDs for efficiency
    );
    $cpt_entries = new WP_Query( $args );

    // If there are posts, retrieve the unique authors
    if ( $cpt_entries->have_posts() ) {
    $authors = array();
    foreach ( $cpt_entries->posts as $post_id ) {
    $author_id = get_post_field( 'post_author', $post_id );
    if ( ! empty( $authors[ $author_id ] ) ) {
    ++$authors[ $author_id ]->igmaps_count;
    continue;
    }
    $authors[ $author_id ] = get_userdata( $author_id );

    // Ensure we're on the 'igmap' custom post type
    if ($typenow == 'igmap') {
    $authors = get_users(['who' => 'authors']);
    $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 . '</option>';
    }
    if ( ! empty( $authors ) ) {
    echo '<select name="igmap_author_filter">';
    echo '<option value="">Filter by Author</option>';

    echo '</select>';
    }
    }
    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');
    add_action( 'restrict_manage_posts', 'add_author_filter_to_igmap_list' );

    // Modify the query based on the selected author filter
    function filter_igmap_by_author($query) {
    global $pagenow;
    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'];
    }
    // 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');
    add_filter( 'parse_query', 'filter_igmap_by_author' );

  5. LC43 revised this gist Nov 22, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 10 );
    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 11 );
    function igmaps_cols( $columns ) {
    $columns['author'] = 'Author';
    $columns['date'] = 'Date';
  6. LC43 revised this gist Nov 22, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 999 );
    <?php
    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 10 );
    function igmaps_cols( $columns ) {
    $columns['author'] = 'Author';
    $columns['date'] = 'Date';
  7. LC43 created this gist Nov 22, 2023.
    62 changes: 62 additions & 0 deletions admin_cols_filters.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@

    add_filter( "manage_igmap_posts_columns", 'igmaps_cols' , 999 );
    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') {
    $authors = get_users(['who' => 'authors']);

    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 . '</option>';
    }

    echo '</select>';
    }
    }
    }
    add_action('restrict_manage_posts', 'add_author_filter_to_igmap_list');

    // 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');