'Page Categories', 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_in_rest' => true, ]); } add_action('init', 'add_page_categories_taxonomy'); // For WP-CLI (where 'init' may not trigger reliably) if (defined('WP_CLI') && WP_CLI) { WP_CLI::add_hook('after_wp_load', function () { register_page_category_taxonomy(); }); } // Add a new column to the Pages list add_filter('manage_page_posts_columns', 'add_page_category_column'); function add_page_category_column($columns) { $new_columns = array(); foreach ($columns as $key => $value) { $new_columns[$key] = $value; if ('author' === $key) { $new_columns['page_category'] = 'Categories'; } } return $new_columns; } // Populate the custom column with taxonomy terms add_action('manage_page_posts_custom_column', 'show_page_category_column', 10, 2); function show_page_category_column($column, $post_id) { if ('page_category' === $column) { $terms = get_the_terms($post_id, 'page_category'); if (!empty($terms) && !is_wp_error($terms)) { $links = array(); foreach ($terms as $term) { $url = admin_url('edit.php?post_type=page&page_category=' . $term->slug); $links[] = '' . esc_html($term->name) . ''; } echo implode(', ', $links); } else { echo '—'; } } } // filter taxonomy add_action('restrict_manage_posts', 'filter_page_by_top_level_category'); function filter_page_by_top_level_category() { global $typenow; if ($typenow !== 'page') return; $taxonomy = 'page_category'; $terms = get_terms([ 'taxonomy' => $taxonomy, 'parent' => 0, // 👈 Only top-level categories 'hide_empty' => false, ]); if (!empty($terms)) { echo ''; } } add_filter('parse_query', 'apply_top_level_category_filter'); function apply_top_level_category_filter($query) { global $pagenow; if ( $pagenow === 'edit.php' && $query->is_main_query() && isset($_GET['page_category']) && !empty($_GET['page_category']) && $query->get('post_type') === 'page' ) { $taxonomy = 'page_category'; $term_slug = sanitize_text_field($_GET['page_category']); // Get full term object by slug $term = get_term_by('slug', $term_slug, $taxonomy); if ($term && !is_wp_error($term)) { $query->set('tax_query', [[ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => [$term->term_id], 'include_children' => true, // 👈 Includes all subcategories too 'operator' => 'IN' // Matches pages with this term, even with others ]]); } } } // 1️⃣ Add the Last Modified column add_filter('manage_page_posts_columns', function($columns) { $columns['modified_date'] = 'Last Modified'; return $columns; }); // 2️⃣ Display the modified time add_action('manage_page_posts_custom_column', function($column, $post_id) { if ($column === 'modified_date') { echo get_post_modified_time('F j, Y g:i a', false, $post_id); } }, 10, 2); // 3️⃣ Make it sortable add_filter('manage_edit-page_sortable_columns', function($columns) { $columns['modified_date'] = 'modified_date'; return $columns; }); // 4️⃣ Handle the sorting logic add_action('pre_get_posts', function($query) { if (!is_admin() || !$query->is_main_query()) { return; } if ($query->get('orderby') === 'modified_date') { $query->set('orderby', 'modified'); } }); // Taxonomy based templates add_action('admin_init', 'generate_page_category_templates'); function generate_page_category_templates() { // Define plugin directory and template folder $plugin_dir = plugin_dir_path(__FILE__); $template_dir = $plugin_dir . 'templates/'; if (!is_dir($template_dir)) { mkdir($template_dir); // Create if not exists } // Get all top-level terms $terms = get_terms([ 'taxonomy' => 'page_category', 'parent' => 0, 'hide_empty' => false, ]); foreach ($terms as $term) { $slug = $term->slug; $filename = "taxonomy-page_category-{$slug}.php"; $filepath = $template_dir . $filename; if (!file_exists($filepath)) { // Create a basic scaffold file file_put_contents($filepath, "name}'\nget_header();\n?>

Category: name}'); ?>

\">

"); } } }