1

I'm using the CPT-onomies plugin to make a CPT into a taxonomy. I have a hero image CPT (created with CPT-onomies) and the hero images post type is attached to the page post type. This allows me to select a hero image post that will be a term of a page post. All of this is working fine. When I visit the page with the hero image as a taxonomy the hero image appears as desired.

What I'm trying to accomplish is that if I'm on a page whose ancestor has a hero image attached, I want the page to show the hero image attached to the parent. Below is my code. I get an array of ancestors and iterate through the array. The image shows up as desired but above the image this error appears:

Notice: Undefined property: stdClass::$data in .../wp-includes/category-template.php on line 1176

//Get the current Post ID
$current_post_id = get_the_id();

//Get the current Post's ancestor's ID's
$current_post_parents = get_ancestors($current_post_id, 'page');


foreach ( $current_post_parents as $post_parent ) {

      //If a hero image ID is associated with the current post being viewed (or a parent post of the current post) assign that hero image ID value to $hero_image_id
      $hero_image_id = isset( $post_parent ) && ( $assigned_hero_images = get_the_terms( $post_parent, 'hero_images' ) ) && is_array( $assigned_hero_images ) && ( $hero_image = array_shift( $assigned_hero_images ) ) && isset( $hero_image->term_id ) ? $hero_image->term_id : NULL;

      if ($hero_image_id)
         break;

} //Code follows that displays the image based on the $hero_image_id

It's the get_the_terms() function that's throwing the error. Line 1176 of the category-template.php file is $to_cache[ $key ] = $term->data;

To be clear, the notice only appears when I'm visiting a child page whose ancestor has a hero image attached. If I'm visiting the page on which the hero image is attached or visiting a page with no hero image attached on the page or any ancestor page then no error appears.

2 Answers 2

1

I ended up using the wp_get_object_terms() function instead of get_the_terms(). The get_the_terms() function attempts to cache the terms where wp_get_object_terms() does not.

The fixed code to get the hero images CPT's ID is:

$hero_image_id = ( $assigned_hero_images = wp_get_object_terms( $post_parent, 'hero_images' ) ) && ( $hero_image = array_shift( $assigned_hero_images ) ) && isset( $hero_image->term_id ) ? $hero_image->term_id : NULL;
0

I think the problem lies with this line:

$hero_image_id = isset( $post_parent ) && ( $assigned_hero_images = get_the_terms( $post_parent, 'hero_images' ) ) && is_array( $assigned_hero_images ) && ( $hero_image = array_shift( $assigned_hero_images ) ) && isset( $hero_image->term_id ) ? $hero_image->term_id : NULL;

I think I see what you're going for, but PHP is not processing it in the order you think it is.

Let me try to break this down:

isset( $post_parent ) // This isn't necessary since it couldn't not exist and still get into this code block.

$assigned_hero_images = get_the_terms( $post_parent, 'hero_images' )

is_array( $assigned_hero_images ) // again, you just created it, so you don't need to check here.

$hero_image = array_shift( $assigned_hero_images )  // You're trying to access the contents of the first element of the array, but this isn't the best approach.

$hero_image->term_id

A cleaned up version that accomplishes what you're doing above would look like this:

$hero_image_id = get_the_terms( $post_parent, 'hero_images')[0]->$term_id;

The [0] gets you access to the first array element, and ->$term_id gets that property from the object. You can do this without saving each to a variable by a process in php called array dereferencing.

This is not tested so I may have missed something, but hopefully it gets you in the ballpark enough to troubleshoot it out from here.

1
  • Thanks for the input. I really appreciate it! I made some of the changes that you suggested. Unfortunately, this solution didn't work for me because in the WP core code the get_the_terms() function attempts to cache the terms. I posted the answer below.
    – TKEz
    Sep 8, 2016 at 13:33

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.