1

This one has me baffled because the code has worked for the past 2 years but after the most recent WordPress update to 4.7 it thru a fit.

WordPress is throwing the error Catchable fatal error: Object of class stdClass could not be converted to string in on line 42 of functions.php which happens to be this line:

WHERE {$wpdb->terms}.term_id = {$term_id}

in the chunk of code below. Any help would be much appreciated.

// Detects Category ID for category Info custom post type
function wph_wp() {
global $wpdb, $cc_post_id;

$terms = null;
$cc_post_id = null;

if (is_category()) {
    $term = get_queried_object();
    $terms = array($term->term_id);
} elseif (is_single()) {
    $post = get_queried_object();
    $terms = wp_get_post_categories($post->ID);
}

if (!empty($terms)) {
    foreach ($terms as $term_id) {
        $cc_post_id = $wpdb->get_var($wpdb->prepare("
            SELECT {$wpdb->posts}.ID FROM {$wpdb->terms} 
            JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id AND {$wpdb->term_taxonomy}.taxonomy = 'category'
            JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
            JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.ID AND post_type = 'category_info'
            WHERE {$wpdb->terms}.term_id = {$term_id}
        ", ""));
        if (!empty($cc_post_id)) {
            break;
        }
    }
}
}
0

Not sure if there was a change in 4.7, but wp_get_post_categories returns an array of category objects, not IDs, so when you loop over the $terms, $term_id is actually an object.

Add the argument 'fields' => 'ids' to the wp_get_post_categories-call and you should be good to go – it'll return an array of category IDs instead.

$terms = wp_get_post_categories($post->ID, ['fields' => 'ids']);
  • Strangely the issue corrected itself but you had a good call out in something I should be doing differently. Thank you. – Dave Bergschneider Dec 9 '16 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.