WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#15449 closed defect (bug) (fixed)

Notice generated in get_category_link function with a category id that does not exist

Reported by: kapilchugh Owned by:
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.0.1
Component: Warnings/Notices Keywords: get_category_link, notice
Focuses: Cc:

Description

Today i was using get_category_link() function which gives category link corresponding to a category id.By mistake i took such category id that was not there in my categories list.

So I was getting one error
Notice: Trying to get property of non-object in C:\xampp\htdocs\wordpress\wp-includes\category-template.php on line 28

Change History (7)

comment:1 hakre3 years ago

Thanks for reporting the issue.

get_category() is used in get_category_link() which should have returned a WP_Error if it's not possible to get the category.

Can you post that part of your code that is triggering the notice (e.g. the line containing your get_category_link() so that it's understandable which arguments have been used? This would also help to analyze and reproduce the issue.

comment:2 nacin3 years ago

  • Milestone changed from Awaiting Review to 3.1

get_term_link() can return a WP_Error. Before the code was cleaned up in [15825], it still could have turned WP_Error.

Improving the docs and leaving it at that.

comment:3 nacin3 years ago

Actually, I see the other issue -- that get_term() (and thus get_taxonomy()) can also return null.

Additional defensive checks might be necessary here.

comment:4 nacin3 years ago

Not anymore, get_term_link() includes an !is_object() check. You won't see a notice anymore, you'll get a WP_Error either way.

comment:5 kapilchugh3 years ago

I did this thing

<?php
$cat_id = '';
$cat_id = get_option('kp_sidebar_cat2');
$cat_name = get_cat_name( $cat_id );
$cat_link = get_category_link( $cat_id );
?>

Somehow i was getting value of $cat_id 4 but there was no such category with this id and because of this i was getting this notice.

Notice: Trying to get property of non-object in C:\xampp\htdocs\wordpress\wp-includes\category-template.php on line 28 

Reason is :-

When i checked i found et_category_link() calls get_category() function and 25th line of category-template.php is this one

$category = &get_category( $category_id );

and $category was returning NULL.

When i changed the condition on 26th line of category-template.php with this condition

if ( is_null($category) || is_wp_error( $category ) )


After that everything was right.I think wp_error is not handling NULL statement.

comment:6 hakre3 years ago

Like Nacin wrote, this should be fixed in 3.1-alpha. To verify this, you can do a test against the current development version:

Info: http://wordpress.org/download/nightly/

Zip-Download: http://wordpress.org/nightly-builds/wordpress-latest.zip

Don't overwrite your life site as the nightly version can break stuff.

comment:7 ryan3 years ago

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.