Make WordPress Core

Opened 3 months ago

#43881 new defect (bug)

url_to_postid doesn't works with custom post type in PhpUnit Test

Reported by: skarabeq Owned by:
Milestone: Awaiting Review Priority: normal
Severity: blocker Version:
Component: General Keywords:
Focuses: Cc:


In the functions.php I have the following code:

function post_type_products()
            'labels' => [
                'name'               => 'products',
                'singular_name'      => 'products',
                'add_new'            => 'Add New',
                'add_new_item'       => 'Add New Product',
                'edit_item'          => 'Edit Product',
                'new_item'           => 'New Product',
                'all_items'          => 'All Product',
                'view_item'          => 'View Product',
                'search_items'       => 'Search for Product',
                'not_found'          => 'No product found',
                'not_found_in_trash' => 'No product found in the Trash',
                'menu_name'          => 'products',
                'parent_item_colon'  => '',
            'description'   => 'Product description',
            'public'        => true,
            'rewrite'       => ['slug' => 'products'],
            'menu_position' => 5,
            'supports'      => ['title', 'editor', 'excerpt'],
            'has_archive'   => true,
            'menu_icon'     => 'dashicons-id-alt',

add_action('init', 'post_type_products');

Then I need to test it. And for this test I create the test record via factory class:

$post_data = [
            'post_title'     => 'Test Post',
            'post_content'   => 'Test Post Content',
            'post_excerpt'   => 'Test Post excerpt',
            'post_type'      => 'products'


The problem is when I got URL from following code: $url = get_permalink($this->factory->post->create($post_data)); The function url_to_postid return 0

$id = url_to_postid($url) // this will return 0;

This is big problem because in my backend code I use the URL of the custom post type for determinate the ID of this URL. When it works outside of the context of the unit test it's work very well. And I can got ID from the URL from custom post type products. But when this is in the unit test context the function url_to_postid doesn't work as well.

Change History (0)

Note: See TracTickets for help on using tickets.