Open chuckreynolds opened 2 years ago
To me that's a must. I'm doing a room-1, room-2, room-3, kind of thing
This works
<?php
// Plugin Name: Admin Sortable Slug Column
// Description: Adds page url path to the admin columns on edit screens.
if ( ! is_admin() ) {
return false;
}
add_filter('manage_pages_columns', function($columns) {
return array_merge( $columns , ['slug' => 'Slug'] );
});
add_action('manage_pages_custom_column', function($column_key, $post_id) {
if ($column_key == 'slug') {
$post = get_post($post_id);
echo $post->post_name;
}
}, 10, 2);
add_filter('manage_edit-page_sortable_columns', function($columns) {
$columns['slug'] = 'slug';
return $columns;
});
add_action('pre_get_posts', function($query) {
$orderby = $query->get('orderby');
if ($orderby == 'slug') {
$query->set('orderby', 'name');
}
});
Hey there, in one of the reviews on the WordPress.org forum i saw another user (@florisassies) suggesting to include a sort function.
Now i’m no coder but i asked ChatGPT and it tweaked the plugin and the code below seems to work:
<?php
/**
* Admin Slug Column (Streamlined Version)
*
* @package Admin_Slug_Column_Simplified
* @wordpress-plugin
* Plugin Name: Admin Slug Column (Streamlined)
* Description: Adds a sortable "Slug" column to the admin posts/pages list without additional columns.
* Version: 1.0
* Author: Adapted Plugin
*/
// If this file is called directly, abort
if ( ! defined( 'WPINC' ) ) {
die;
}
// Only run plugin in the admin
if ( ! is_admin() ) {
return;
}
// Add slug column to admin posts/pages lists
function add_slug_column($columns) {
$columns['slug'] = __('Slug');
return $columns;
}
add_filter('manage_posts_columns', 'add_slug_column');
add_filter('manage_pages_columns', 'add_slug_column');
// Populate slug column with the post slug
function display_slug_column_content($column, $post_id) {
if ($column === 'slug') {
$post = get_post($post_id);
echo esc_html($post->post_name); // Display the post slug
}
}
add_action('manage_posts_custom_column', 'display_slug_column_content', 10, 2);
add_action('manage_pages_custom_column', 'display_slug_column_content', 10, 2);
// Make slug column sortable
function add_slug_column_sortable($columns) {
$columns['slug'] = 'slug';
return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'add_slug_column_sortable');
add_filter('manage_edit-page_sortable_columns', 'add_slug_column_sortable');
// Modify the query to sort by slug when requested
function sort_by_slug_column($query) {
if (!is_admin() || !$query->is_main_query()) {
return;
}
$orderby = $query->get('orderby');
if ('slug' == $orderby) {
$query->set('orderby', 'name'); // 'name' represents the slug in the database
}
}
add_action('pre_get_posts', 'sort_by_slug_column');
Would be a good option - not a necessity. Would need to consider prioritizing and grouping parent page/post names in order too.