WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#17336 closed defect (bug) (invalid)

Can't use get_the_terms() within a function that hooks into publish_post

Reported by: defunctlife Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

I've tried everything, but get_the_terms() always returns false. When using a similar function on the actual single.php page it works fine. I have confirmed the correct post id's are being returned within the dashboard. I have also confirmed that indeed these posts have a taxonomy item of myshift_title from within the dashboard. I tried wp_get_object_terms() but it returns an empty array.

From what I can tell I just can access a post's taxonomy, and I would really like to.

From my functions.php

function shift_add_progress_report_notification($post_ID) {		

	/* Register the taxonomy as we don't seem to have access to it here */	
	register_taxonomy('myshift_title', array('myshift', 'post'), array('label' => 'My Shift Title', 'rewrite' => false, 'query_var' => true));

	$terms = get_the_terms( $post_ID, 'myshift_title');

	ob_start();
	var_dump($terms);
	$a = ob_get_contents();
	ob_end_clean();
	
	mail('xxxxxxx', 'Progress Report', $a . ' POST ID: ' . $post_ID);
}
add_action( 'publish_post', 'shift_add_progress_report_notification', 10, 1);

Change History (4)

comment:1 @defunctlife4 years ago

removed comment.

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

comment:2 @defunctlife4 years ago

This appears to be an issue as I am posting this post with ajax...still investigating. Found a work around in my case not using any hooks and just calling the function manually where we create the post.

comment:3 @wonderboymusic3 years ago

  • Resolution set to invalid
  • Status changed from new to closed

I think you have some code in weird places - this works:

add_action( 'init', function () {
	register_taxonomy( 'custom_thing', 'post', array( 'labels' => array( 'name' => 'Woo Hoo' ) ) );
} );

var_dump( $terms );

function shift_add_progress_report_notification( $post_ID ) {		

	$terms = get_the_terms( $post_ID, 'custom_thing' );

	var_dump( $terms );
	exit();
}
add_action( 'publish_post', 'shift_add_progress_report_notification', 10, 1 );
Version 1, edited 3 years ago by wonderboymusic (previous) (next) (diff)

comment:4 @SergeyBiryukov3 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.