0

I'm trying to retrieve the terms of a page's custom taxonomy.

When I'm in a post this works just fine:

$args=array('orderby' => 'none');
$terms = wp_get_post_terms( $post->ID , 'custom_taxonomy', $args);

But this won't work on a page.

I've tried also with: get_the_terms & wp_get_object_terms

But they don't seem to work.

BTW, I'm using a hierarchical taxonomy.

Thanks for any help in seeing the light ;-)

2 Answers 2

1

Verify that $post->ID is set and of expected value.

What don't seem to work means? What is actual return of function(s)?

1

Hi @Rodrigo Pontremoli:

I am finding that with the following code in my theme's functions.php file that wp_get_object_terms($post->ID,'page-type',array('orderby' => 'none')) is in fact working in my theme's page.php template file:

add_action('init','page_terms_init');
function page_terms_init() {
  register_taxonomy('page-type', 'page', array(
    'hierarchical'    => true,
    'label'           => 'Page Type',
    'query_var'       => 'page-type',
    'rewrite'         => array('slug'=>'page-types' ),
    )
  );
  // This should really be done in a plugin activation
  global $wp_rewrite;
  $wp_rewrite->flush_rules(false);  
}

So, I figure there are one of two things happening:

  1. It is working for you but the format of the return values are not what you expect, or

  2. You have a plugin that is causing it to fail.

  3. When you are calling wp_get_object_terms() that $post doesn't have a value.

If it is #1 then this is what my test data looks like:

Array
(
  [0] => stdClass Object
    (
      [term_id] => 140
      [name] => info
      [slug] => info
      [term_group] => 0
      [term_taxonomy_id] => 146
      [taxonomy] => page-type
      [description] => 
      [parent] => 0
      [count] => 1
    )

  [1] => stdClass Object
    (
      [term_id] => 141
      [name] => pics
      [slug] => pics
      [term_group] => 0
      [term_taxonomy_id] => 147
      [taxonomy] => page-type
      [description] => 
      [parent] => 0
      [count] => 1
    )

  [2] => stdClass Object
    (
      [term_id] => 142
      [name] => vids
      [slug] => vids
      [term_group] => 0
      [term_taxonomy_id] => 148
      [taxonomy] => page-type
      [description] => 
      [parent] => 0
      [count] => 1
    )

  [3] => stdClass Object
    (
      [term_id] => 143
      [name] => Flickr
      [slug] => flickr
      [term_group] => 0
      [term_taxonomy_id] => 149
      [taxonomy] => page-type
      [description] => 
      [parent] => 141
      [count] => 1
    )

  [4] => stdClass Object
    (
      [term_id] => 144
      [name] => Other
      [slug] => other
      [term_group] => 0
      [term_taxonomy_id] => 150
      [taxonomy] => page-type
      [description] => 
      [parent] => 141
      [count] => 1
    )

  [5] => stdClass Object
    (
      [term_id] => 145
      [name] => Youtube
      [slug] => youtube
      [term_group] => 0
      [term_taxonomy_id] => 151
      [taxonomy] => page-type
      [description] => 
      [parent] => 142
      [count] => 1
    )

  [6] => stdClass Object
    (
      [term_id] => 146
      [name] => Other
      [slug] => other-vids
      [term_group] => 0
      [term_taxonomy_id] => 152
      [taxonomy] => page-type
      [description] => 
      [parent] => 142
      [count] => 1
    )

)

If it is #2 then try disabling your plugins one-by-one until you find the culprit.

If #3 then you'll need to move your code to where $post->ID does have a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.