Make WordPress Core

Opened 14 years ago

Closed 14 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's profile kapilchugh Owned by:
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.0.1
Component: Warnings/Notices Keywords: get_category_link, notice
Focuses: Cc:


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)

#1 @hakre
14 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.

#2 @nacin
14 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.

#3 @nacin
14 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.

#4 @nacin
14 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.

#5 @kapilchugh
14 years ago

I did this thing

$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.

#6 @hakre
14 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:



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

#7 @ryan
14 years ago

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