Opened 11 years ago
Closed 11 years ago
#28572 closed defect (bug) (duplicate)
Modify get_the_ID() function to return false, if get_post() returns null, otherwise it generates a PHP warning
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.1 |
Component: | Posts, Post Types | Keywords: | |
Focuses: | Cc: |
Description
To replicate the issue, a simple use case would be, add this to functions.php:
add_action ( 'wp_enqueue_scripts', 'us_enqueue_myscripts' );
function us_enqueue_myscripts() {
$post_id = 5; //Any random post id here
if ( get_the_ID() != $post_id ) {
return;
}
}
There are NO POSTS, If you access wp-signup.php in case of multisite, get_post() would return null and as a result get_the_ID() generates a PHP Notice: Trying to get property of non-object
Another strange behavior I came across while testing this, get_post() returns a latest Post ID on wp-signup.php, instead it should have returned null.
There are no additional plugins or themes, just the defaults one.
Attachments (1)
Change History (2)
Note: See
TracTickets for help on using
tickets.
Fixes get_the_ID() if get_post() returns null