WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16593 closed defect (bug) (worksforme)

wp_set_object_terms not creating links when in a save_post action

Reported by: grandslambert Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.5
Component: General Keywords: reporter-feedback
Focuses: Cc:

Description

I have a custom post type and a custom taxonomy for a recipe plugin. The edit page for the post type has a meta box where ingredients are added, and the ingredient name is the custom taxonomy. I have a hook into the 'save_post' action that then calls a function to save the ingredients. Within that function is code to loop through and either match an existing term, or create a new term in the taxonomy. This part works without a problem. While doing this, I create an array of data related to the term to use in wp_set_object_terms().

I then call:

wp_set_object_terms($post_id, $postIngredients, 'recipe-ingredient', false);

where $post_id is the ID of the post, and $postIngredients is the data I collected earlier. I have tried an array of term IDs, names, and slugs, all with the same result. Even though wp_set_object_terms() returns a list of "affected" IDs, the data does not appear to be saved. When I go back to the edit screen for that post, it does not show an updated list of ingredients.

The code is included in the RecipePress plugin and appears in the /classes/recipe-press-core.php file.

I also have an importer and front end form that uses the same function to save the ingredients. Both of these do exactly what they are supposed to do. It just does not work when the function is called from the 'save_post" action.

Change History (5)

comment:1 greuben3 years ago

  • Keywords reporter-feedback added

I can’t reproduce the issue…I am using the code below to create tags and it works fine

add_action( 'save_post', 'my_test' );
function my_test( $post_id ) {
	global $post;
	if( ! isset( $post->post_type ) || $post->post_type != 'post' )
		return $post_id;

	wp_set_object_terms( $post_id, array( 'test1', 'test2' ), 'post_tag' );
	return $post_id;
}

comment:2 grandslambert3 years ago

For what it's worth, I have deteremined that my save_post action only appears to run when saving revisions, as the $post_id I get in the action is not the same as the post ID for the post I just edited. Within the save action I call a function

$this->save_ingredients($post_id, $_POST['ingredients']);

Which sends the revision post ID and not the actual post ID. So, I modified my save_ingredients function to apply the objects to both the $post_id sent to the function, and the global $post->ID and this seems to work. I use several 'add_post_meta' calls and they all work, but if I send the $post_id to my own function, it does not.

Why does the save action only have the revision ID and not the post ID? Is this the intended result?

comment:3 nacin3 years ago

save_post runs for both. You need to check the post type of the post passed in, and bail for revisions. The regular post runs second I think.

comment:4 grandslambert3 years ago

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

DUH, I knew I was missing something simple. Thanks - bailing on revisions fixes the problems.

comment:5 scribu3 years ago

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