Opened 11 years ago
Closed 11 years ago
#30879 closed defect (bug) (wontfix)
wp_delete_object_term_relationships triggers PHP warning when object has no terms in taxonomy
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.1 |
| Component: | Taxonomy | Keywords: | reporter-feedback |
| Focuses: | Cc: |
Description
If using wp_delete_object_term_relationships on an onbject with a taxonomy, where the object does not have any terms in that taxonomy, the function triggers a PHP Warning, sicne it's trying to map an array, where there is none.
The function body is like this:
$object_id = (int) $object_id;
if ( !is_array($taxonomies) )
$taxonomies = array($taxonomies);
foreach ( (array) $taxonomies as $taxonomy ) {
$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
$term_ids = array_map( 'intval', $term_ids );
wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
}
But should probably be like this:
$object_id = (int) $object_id;
if ( !is_array($taxonomies) )
$taxonomies = array($taxonomies);
foreach ( (array) $taxonomies as $taxonomy ) {
$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
if ( empty( $term_ids ) {
continue;
}
$term_ids = array_map( 'intval', $term_ids );
wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
}
Change History (5)
#3
@
11 years ago
- Keywords reporter-feedback added
Actually, I take back the "confirmed". It turns out I could reproduce a PHP notice only if I used an invalid taxonomy (in which case wp_get_object_terms() returns a WP_Error object). In all other cases, wp_get_object_terms() returns an array, even if it's an empty array.
ragulka, could you please double check what's happening here? Exactly what value is being passed to array_map()? If it turns out that it's a WP_Error object, we're going to have to get creative here, since the error provides a valuable notice to developers, but wp_delete_object_term_relationships() doesn't have a return value.
#4
@
11 years ago
boonebgorges, I think you're right. I don't have the environment where I encountered this available, but I think I might have called the function before init, which means that my custom taxonomy wasn't registered yet.
#5
@
11 years ago
- Milestone 4.2 deleted
- Resolution set to wontfix
- Status changed from new to closed
@ragulka - Thanks for reporting back. On that assumption, I'm going to close this ticket. This is the kind of PHP notice that actually does a service to the developer, by letting them know that something is wrong with their code instead of failing silently.
Confirmed. Thanks for the report.