WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#39129 closed defect (bug) (invalid)

get_the_terms seems to be broken after upgrading to version 4.7

Reported by: kuphd1 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: reporter-feedback close
Focuses: Cc:

Description

my code:

<?php
foreach((get_the_terms($post->ID, 'states')) as $term) {
$state_name = $term->name;
$state_slug = $term->slug;
}
?>

...returns an "Invalid argument supplied for foreach()" error on the page linked below. (Scroll down the page to view the error.)

http://marriages.gmsdevsite.com/who-we-are/local-state-chapters-and-megs/

Change History (15)

#1 @dd32
4 years ago

  • Component changed from General to Taxonomy
  • Keywords reporter-feedback close added

Hi @kuphd1,

Can you please post an example of code which will fail, including all the dependencies (such as registration of the taxonomy)?

You may want to inspect the return value of get_the_terms() to determine what the error condition being hit is.

#2 follow-up: @jonathan176
4 years ago

  • Severity changed from normal to major

I am getting this too! I am trying to pull the terms of a custom post type. Im getting an array, but not of terms. Below is an output of

get_the_terms($Post->ID,$taxname);

returns

Array(
    [_edit_last] => Array([0] => 2)
    [dd_biosphere_position] => Array([0] =>  Description)
    [layout] => Array ([0] => full_width)
    [slide_template] => Array([0] => default)
    [enable_carousel] => Array([0] => false)
    [posts_to_show] => Array([0] => -1)
    [posts_description_length] => Array([0] => 100)
    [_edit_lock] => Array([0] => 1449370184:2)
    [body_classes] => Array([0] =>)
    [_body_classes] => Array([0] => field_548a4a569d990)
    [wp_featherlight_disable] => Array([0] =>)
    [_thumbnail_id] => Array([0] => 13084)
)

#3 @kuphd1
4 years ago

Hi @dd32,

Here is how the taxonomy is registered:

<?php

// Chapters Taxonomy
add_action('init', 'cptui_register_my_taxes_states');
function cptui_register_my_taxes_states() {
register_taxonomy( 'states',array (
0 => 'chapters',
),
array( 'hierarchical' => true,
'label' => 'States',
'show_ui' => true,
'query_var' => true,
'show_admin_column' => false,
'labels' => array (
'search_items' => 'State',
'popular_items' => 'Popular States',
'all_items' => 'All States',
'parent_item' => 'Parent State',
'parent_item_colon' => 'Parent State:',
'edit_item' => 'Edit State',
'update_item' => 'Update State',
'add_new_item' => 'Add New State',
'new_item_name' => 'New State Name',
'separate_items_with_commas' => 'Separate states with commas',
'add_or_remove_items' => 'Add or remove states',
'choose_from_most_used' => 'Choose from the most used states',
)
) );
}
?>

When I run this:

<?php
$terms_test = get_the_terms($post->ID, 'states');
print_r($terms_test);
?>

...it's empty which is why I'm getting the foreach error.

Hope that helps.

Thanks.

#4 @mensmaximus
4 years ago

  • Severity changed from major to trivial

Can't confirm any issue with get_the_terms(). I am using it heavily in some of my directory applications:

get_the_terms( $this_id, 'treatment' )

Array
                (
                    [0] => WP_Term Object
                        (
                            [term_id] => 60
                            [name] => Body
                            [slug] => body
                            [term_group] => 0
                            [term_taxonomy_id] => 60
                            [taxonomy] => treatment
                            [description] => 
                            [parent] => 0
                            [count] => 2
                            [filter] => raw
                            [term_order] => 0
                        )

                    [1] => WP_Term Object
                        (
                            [term_id] => 61
                            [name] => hair transplant
                            [slug] => hair-transplant
                            [term_group] => 0
                            [term_taxonomy_id] => 61
                            [taxonomy] => treatment
                            [description] => 
                            [parent] => 0
                            [count] => 1
                            [filter] => raw
                            [term_order] => 0
                        )

                    [2] => WP_Term Object
                        (
                            [term_id] => 51
                            [name] => FUE hair transplant
                            [slug] => fue-hair-transplant
                            [term_group] => 0
                            [term_taxonomy_id] => 51
                            [taxonomy] => treatment
                            [description] => 
                            [parent] => 0
                            [count] => 60
                            [filter] => raw
                            [term_order] => 1
                        )

                    [3] => WP_Term Object
                        (
                            [term_id] => 54
                            [name] => Facial hair transplant
                            [slug] => facial-hair-transplant
                            [term_group] => 0
                            [term_taxonomy_id] => 54
                            [taxonomy] => treatment
                            [description] => 
                            [parent] => 0
                            [count] => 26
                            [filter] => raw
                            [term_order] => 4
                        )

                    [4] => WP_Term Object
                        (
                            [term_id] => 55
                            [name] => Eyebrow transplant
                            [slug] => eyebrow-transplant
                            [term_group] => 0
                            [term_taxonomy_id] => 55
                            [taxonomy] => treatment
                            [description] => 
                            [parent] => 0
                            [count] => 27
                            [filter] => raw
                            [term_order] => 5
                        )

                )

#5 in reply to: ↑ 2 @jonathan176
4 years ago

  • Severity changed from trivial to major

Replying to jonathan176:

I am getting this too! I am trying to pull the terms of a custom post type. Im getting an array, but not of terms. Below is an output of

get_the_terms($Post->ID,$taxname);

returns

Array(
    [_edit_last] => Array([0] => 2)
    [dd_biosphere_position] => Array([0] =>  Description)
    [layout] => Array ([0] => full_width)
    [slide_template] => Array([0] => default)
    [enable_carousel] => Array([0] => false)
    [posts_to_show] => Array([0] => -1)
    [posts_description_length] => Array([0] => 100)
    [_edit_lock] => Array([0] => 1449370184:2)
    [body_classes] => Array([0] =>)
    [_body_classes] => Array([0] => field_548a4a569d990)
    [wp_featherlight_disable] => Array([0] =>)
    [_thumbnail_id] => Array([0] => 13084)
)

#6 @boonebgorges
4 years ago

In 4.7, the internals of wp_get_object_terms() were swapped out so that get_terms() is used, rather than a custom SQL query. So my hunch is that another plugin is interfering with general term queries in a way that is causing problems for get_the_terms(). @kuphd1 and @jonathan176, can you please look through your plugins and themes for anything that's filtering the following:

  • terms_clauses
  • get_terms_args
  • get_terms

Another helpful debugging tool would be to inspect the SQL query being executed in WP_Term_Query::get_terms() ($this->request, assembled around line 664 of wp-includes/class-wp-term-query.php). At least in @jonathan176's case, something very odd is happening, and this will give some helpful hints.

It may be necessary to disable your persistent cache (if you're running one) when debugging; many of these queries and filters are not run when Memcached etc is enabled. (On that note, a good starting place in debugging might be a simple cache flush.)

#7 follow-up: @kuphd1
4 years ago

  • Severity changed from major to normal

@boonebgorges... thanks for reminding me of the first troubleshooting step that I know I should've taken. checking the plugins. In my case, it was a plugin called Anything Order. Disabling that plugin caused everything to go back to normal. Now I just have to find something that will do the same thing it does, because it's a fairly integral part of some of the sites I administer.

#8 @boonebgorges
4 years ago

@kuphd1 - Thanks for reporting back. I've had a look at Anything Order and I reproduced some SQL errors, which I assume is what's causing your problem. It doesn't look like that plugin is maintained anymore. As a workaround, you can probably fix the issue by changing the table alias that the plugin uses for the term relationships table (it's a clash with WP that's causing the error). Open anything-order/modules/taxonomy/class.php and change all instances of "tr." to "tr2.".

@jonathan176 When you get a chance to look into the root cause of your issue, please let us know in this thread.

#9 @kuphd1
4 years ago

@boonebgorges... thanks much for the help. Very much appreciate it. I'll give that a shot and report back. :)

#10 in reply to: ↑ 7 ; follow-up: @jonathan176
4 years ago

I too have the Anything Order plugin. I had to rollback so I can't test this right away. But I will report back in as soon as I have a sandbox running.

Thanks for all the help.

Replying to kuphd1:

@boonebgorges... thanks for reminding me of the first troubleshooting step that I know I should've taken. checking the plugins. In my case, it was a plugin called Anything Order. Disabling that plugin caused everything to go back to normal. Now I just have to find something that will do the same thing it does, because it's a fairly integral part of some of the sites I administer.

Last edited 4 years ago by jonathan176 (previous) (diff)

#11 @briarinc
4 years ago

I'm author plugin Anything Order by Terms https://wordpress.org/plugins/anything-order-by-terms/. I fix some bug in version 1.2.0: change table alias in SQL filter and using termmeta table instead term_relationships.

Please test if you have issue with Anything Order.

Last edited 4 years ago by briarinc (previous) (diff)

#12 @kuphd1
4 years ago

Hi @briarinc,

The plugin I was referring to is the Anything Order plugin found at https://wordpress.org/plugins/anything-order/. I believe this one has a different developer.

#13 @kuphd1
4 years ago

@boonebgorges... your solution definitely works. But it's not just the "tr." that need to change. It's every instance of "tr" (without the ".") in all the custom queries. So...

<?php
$query = "SELECT tr.term_order FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id LEFT JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE t.term_id = %d AND tt.taxonomy = %s AND tr.object_id = 0";
?>

becomes...

<?php
$query = "SELECT tr2.term_order FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id LEFT JOIN $wpdb->term_relationships AS tr2 ON tr2.term_taxonomy_id = tt.term_taxonomy_id WHERE t.term_id = %d AND tt.taxonomy = %s AND tr2.object_id = 0";
?>

Thank you again. I never would've figured that out. :)

#14 in reply to: ↑ 10 @jonathan176
4 years ago

It was the "Anything Order" plugin causing the problem. I have it replaced it with the above mentioned "Anything Order by Terms" and it works great!

Thank you all for your help!

Replying to jonathan176:

I too have the Anything Order plugin. I had to rollback so I can't test this right away. But I will report back in as soon as I have a sandbox running.

Thanks for all the help.

Replying to kuphd1:

@boonebgorges... thanks for reminding me of the first troubleshooting step that I know I should've taken. checking the plugins. In my case, it was a plugin called Anything Order. Disabling that plugin caused everything to go back to normal. Now I just have to find something that will do the same thing it does, because it's a fairly integral part of some of the sites I administer.

#15 @boonebgorges
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thanks to both of you for the feedback. Given that the issue arises from a very edge-case filtering of a plugin that probably ought to have namespaced its table aliases in the first place, I think that no further action is needed in core.

Note: See TracTickets for help on using tickets.