[wp-trac] [WordPress Trac] #41679: get_the_terms does not return expected results

WordPress Trac noreply at wordpress.org
Mon Aug 21 19:47:10 UTC 2017


#41679: get_the_terms does not return expected results
-------------------------------+------------------------------
 Reporter:  rslotb             |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Taxonomy           |     Version:  4.8.1
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by rslotb):

 Well, yes, I debugged through {{{get_the_terms()}}}, which, as you say,
 will get the required value from the cache {{{1202}}}. At least in my
 installation, this is always the case, I never get to {{{1205}}}. So the
 cache must be corrupt. Which it is, in my case.

 The cache is initialised by {{function
 update_object_term_cache($object_ids, $object_type), taxonomy.php, 3084}}.

 And the results coming back from this:
 {{{
 $terms = wp_get_object_terms( $ids, $taxonomies, array(
                 'fields' => 'all_with_object_id',
                 'orderby' => 'name',
                 'update_term_meta_cache' => false,
         ) );
 }}}
 are the problem. As you can see, the query specifies 'all_with_object_id',
 but this is not honoured by the called code. results do come back, but
 they do not contain object_ids.

 After this:
 {{{
 $object_terms = array();
         foreach ( (array) $terms as $term ) {
                 $object_terms[ $term->object_id ][ $term->taxonomy ][] =
 $term->term_id;
         }
 }}}
 My result is:
 {{{
 $object_terms = (
     [] => Array
         (
             [project_category] => Array
                 (
                     [0] => 60
                     [1] => 56
                 )
             [category] => Array
                 (
                     [0] => 4
                 )
         )
 )
 }}}
 And after:
 {{{
 foreach ( $ids as $id ) {
                 foreach ( $taxonomies as $taxonomy ) {
                         if ( ! isset($object_terms[$id][$taxonomy]) ) {
                                 if ( !isset($object_terms[$id]) )
                                         $object_terms[$id] = array();
                                 $object_terms[$id][$taxonomy] = array();
                         }
                 }
         }
 }}}
 It is:
 {{{
 (
     [] => Array
         (
             [project_category] => Array
                 (
                     [0] => 60
                     [1] => 56
                 )

             [category] => Array
                 (
                     [0] => 4
                 )
         )
     [2144] => Array
         (
             [category] => Array
                 (
                 )
             [post_tag] => Array
                 (
                 )
             [project_category] => Array
                 (
                 )
         )
 )
 }}}
 With 2144 being my postid. And this is why the cache is corrupt. That
 second array, without associated terms, is what is put into the cache for
 2144.

 It it is not that converting {{{$terms}}} to {{{WP_Term}}} objects results
 in a null result. But the result is missing the object_ids (one in each
 term). The objects coming back each look like this:
 {{{
 WP_Term Object
         (
             [term_id] => 60
             [name] => Coming soon
             [slug] => coming-soon
             [term_group] => 0
             [term_taxonomy_id] => 60
             [taxonomy] => project_category
             [description] => pre-launch stage
             [parent] => 0
             [count] => 2
             [filter] => raw
         )
 }}}
 Which, I hope is obvious, does not contain an {{{[object_id] => 2144}}}.

 Eliminating the call to: {{{ array_map( 'get_term', $terms )}}} keeps the
 original query result intact, which makes the calling code work as
 expected.

 The problem probably is broader: {{{WP_Query::get_terms()}}} currently
 does not honour the passed in 'all_with_object_id' parameter correctly. If
 there is a unit test for it, I think it needs looking at.

 Regards, and thanks for looking at this,

 Remco.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41679#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list