Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#43881 new defect (bug)

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

Reported by: skarabeq's profile skarabeq Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Rewrite Rules 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 (4)

#1 @skarabeq
6 years ago

@dd32, @subrataemfluence What happens with this ticket ? 8 months without progress ? Are you kidding me ?

#2 @dd32
6 years ago

  • Component changed from General to Rewrite Rules
  • Severity changed from blocker to minor

@skarabeq Please don't ping me in random tickets, that's not a useful way to move tickets forward. As far as bugs go, this is incredibly minor, and if you're affected by it, you should consider submitting some code to help fix this use-case (that evidently not many run into, based on lack of reports and comments here).

#3 @skarabeq
6 years ago

  • Severity changed from minor to blocker

@dd32 , I think it is your responsibility or responsibility of your team to check it.In my code snippet Is already explained how can they can reproduce it. But you and your team know more about procedure function url_to_postid how does it work. More over I don't ping you in random tickets! I ping you in tickets where I have no responsive answers!

#4 @dd32
6 years ago

  • Severity changed from blocker to normal
Note: See TracTickets for help on using tickets.