2

With WordPress version 4.9.4, I'm looking for way to create taxonomy data for pages. If this is possible, I would like to understand (1) how this works, and (2) how to list that taxonomy value under a new column on the /edit.php?post_type=page in wp-admin.

Thanks!

2

This is definitely possible, and pretty easy too!

If you'd like to register an existing taxonomy (i.e. category) with an existing post type (i.e page), register_taxonomy_for_object_type() can be used. E.g.:

add_action( 'init', 'wpse_register_category_tax_for_page' );
function wpse_register_category_tax_for_page() {
    $taxonomy    = 'category';
    $object_type = 'page';

    register_taxonomy_for_object_type( $taxonomy, $object_type );
}

A new taxonomy can be created and associated with an new/existing post type as well. In this example, the taxonomy classification is associated with the existing post type, page:

add_action( 'init', 'wpse_register_custom_taxonomy', 0 );
function wpse_register_custom_taxonomy() {
    // Arguments for register_taxonomy().
    $args = [
        'public'            => true,
        'hierarchical'      => true,
        'label'             => __( 'Classification', 'textdomain' ),
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => 'classification',
        'rewrite'           => [ 'slug' => 'classification' ],
    ];

    // Array of post types to associate with this taxonomy.
    $post_types = [ 'page' ];

    register_taxonomy( 'classification', $post_types, $args );
}

Note that the parameter $show_admin_column has been set to true here. This will ensure that the taxonomy column will be added to the /wp-admin/edit.php?post_type=page screen.

Let's pretend that some other plugin registered the classification taxonomy and set the $show_admin_column parameter to false. We can use the register_taxonomy_args to override the original setting and ensure that the taxonomy appears in the admin columns:

add_filter( 'register_taxonomy_args', 'wpse_edit_taxonomy_args', 10, 3 );
function wpse_edit_taxonomy_args( $args, $taxonomy, $object_type ) {
    // Array of taxonomies to change arguments on.
    $taxonomies = [
        'classification',
    ];

    // Set $show_admin_column to true.
    if ( in_array( $taxonomy, $taxonomies ) ) {
        $args[ 'show_admin_column' ] = true;
    }

    return $args;
}
  • 1
    many many thanks! I will put this to the test on my development server today and get back with you! – klewis Apr 4 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.