Make WordPress Core


Ignore:
Timestamp:
07/07/2020 12:53:41 AM (6 years ago)
Author:
whyisjake
Message:

Taxonomy: Add support for default terms for custom taxonomies.

The new default_term argument is added to register_taxonomy() allowing a user to define the default term name and optionally slug and description.

Fixes #43517.

Props enrico.sorcinelli, SergeyBiryukov, desrosj, davidbaumwald, whyisjake.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/taxonomy.php

    r47186 r48356  
    966966                $this->assertEquals( $problematic_term, $term_name );
    967967        }
     968
     969        /**
     970         * Test default term for custom taxonomy.
     971         *
     972         * @ticket 43517
     973         */
     974        function test_default_term_for_custom_taxonomy() {
     975
     976                wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     977
     978                $tax = 'custom-tax';
     979
     980                // Create custom taxonomy to test with.
     981                register_taxonomy(
     982                        $tax,
     983                        'post',
     984                        array(
     985                                'hierarchical' => true,
     986                                'public'       => true,
     987                                'default_term' => array(
     988                                        'name' => 'Default category',
     989                                        'slug' => 'default-category',
     990                                ),
     991                        )
     992                );
     993
     994                // Add post.
     995                $post_id = wp_insert_post(
     996                        array(
     997                                'post_title' => 'Foo',
     998                                'post_type'  => 'post',
     999                        )
     1000                );
     1001
     1002                $term = wp_get_post_terms( $post_id, $tax );
     1003                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     1004
     1005                // Add custom post type.
     1006                register_post_type(
     1007                        'post-custom-tax',
     1008                        array(
     1009                                'taxonomies' => array( $tax ),
     1010                        )
     1011                );
     1012                $post_id = wp_insert_post(
     1013                        array(
     1014                                'post_title' => 'Foo',
     1015                                'post_type'  => 'post-custom-tax',
     1016                        )
     1017                );
     1018                $term    = wp_get_post_terms( $post_id, $tax );
     1019                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     1020        }
    9681021}
Note: See TracChangeset for help on using the changeset viewer.