[wp-trac] [WordPress Trac] #39926: wp_get_object_terms should return WP_Error on wrong fields argument or use a sane default
WordPress Trac
noreply at wordpress.org
Tue Feb 21 13:40:53 UTC 2017
#39926: wp_get_object_terms should return WP_Error on wrong fields argument or use
a sane default
--------------------------+-----------------------------
Reporter: BjornW | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
wp_get_object_terms( $object_ids, $taxonomies, $args ) accepts object_ids,
taxonomies and as last option extra arguments as an array. One of the
extra arguments in the $args array is the fields option.
I used the field value 'term_id' (erroneously) assuming this would return
the term_ids. However this did not work. wp_get_object_terms returned an
empty array and in my error log I noticed this SQL error message:
{{{
WordPress database error You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax
to use near 'FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON
t.term_id = tt.term_id ' at line 1 for query SELECT FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN
wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('product') AND tr.object_id IN (449, 427) ORDER BY
t.name ASC
}}}
After consulting the source of the wp_get_object_terms I noticed that
'term_id' is not a valid fields value. However I would have expected
wp_get_object_terms() to return a WP_Error object instead of creating
erroneous SQL code.
The following are valid field option values:
* all - Default : all matching term's objects will be returned
* ids : term's ids will be returned
* names : term's names will be returned
* slugs : term's slugs will be returned
* all_with_object_id : all matching term's objects will be returned
* tt_ids : term's taxonomy's ids will be returned
[https://codex.wordpress.org/Function_Reference/wp_get_object_terms See
wp_get_object_terms() docs on the Codex]
=== Proposed solution
Due to [https://core.trac.wordpress.org/browser/trunk/src/wp-includes
/class-wp-term-query.php#L581 this switch statement] invalid or an empty
fields value will result in an empty SELECT SQL query in this
[https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-
term-query.php#L650 codeblock]
My proposal is to add a default clause to the
[https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-
term-query.php#L581 switch statement] which defaults to using the 'all'
case value.
This seems to adhere to the functionality as well as the spirit of the
rest of the WP_Term_Query class code. As far as I know it should be
backwards compatible as well.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39926>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list