gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Categories are not Reloaded after updating the Custom Fields (Page Reload Needed)

Open TachafineAittoudaWebpick opened this issue 3 years ago • 2 comments

Description

The categories block isn't updated after saving the metaboxes, i have save_post hooks depending on the metabox content (checkbox) to add a specific category to the post. the save_post hook is applied but the editor doesn't update the categories in the front-end

Step-by-step reproduction instructions

I was tracing the requests in the network after clicking on the update post button , and they go in the following order :

  • POST : Updating Post Content
  • GET : Updating categories in front-end
  • POST : Updating MetaBox Changes

Screenshots, screen recording, code snippet


// add/remove podcast category  on save post

add_action( 'save_post', 'update_podcast_category_on_save' );

function update_podcast_category_on_save( $post_id ) {
	$podcast          = get_post_meta( $post_id, 'podcasts', true );
	$podcast_category = get_category_by_slug( 'podcast' );
	$post_categories   = wp_get_post_categories( $post_id );
	$cats              = array();
	foreach ( $post_categories as $c ) {
		$cat    = get_category( $c );
		$cats[] = $cat->term_id;
	}
	if ( $podcast['podcast_active'] == 'true' ) {
		if ( ! in_array( $podcast_category->term_id, $cats ) ) {
			wp_set_post_categories( $post_id, array( $podcast_category->term_id ), true );
		}
	} else {
		if ( in_array( $podcast_category->term_id, $cats ) ) {
			unset( $cats[ array_search( $podcast_category->term_id, $cats ) ] );
			wp_set_post_categories( $post_id, $cats );
		}
	}
}

Environment info

  • Wordpress 5.8.2
  • Chrome Version 96.0.4664.110 (Official Build) (64-bit)
  • OS : Ubuntu 20.04 lts

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Thank you for reporting this, @TachafineAittoudaWebpick

Are you still able to reproduce this problem? If so, do you have any suggestion on how we could solve this?

Thank you!

mrfoxtalbot avatar Sep 12 '22 15:09 mrfoxtalbot

@TachafineAittoudaWebpick Checking in again to see whether you can still replicate with the latest version of WordPress, and whether you have any thoughts on a fix. Thanks!

kathrynwp avatar Sep 22 '22 15:09 kathrynwp

This issue was reviewed in today's Editor Bug Scrub. It was determined that this is actually expected behavior with metaboxes. Most metabox implementations will not dynamically change the content in the Editor, and will only appear on a page refresh as you indicated. We understand of course that this does not resolve the issue you are having, but there are no current plans that we are aware of that will remedy this situation. Therefore, I am going to close this issue, but feel free to reopen if you want to propose an alternative solution and/or PR.

In the meantime. I wanted to share some resources for WordPress Core Data and the SlotFill Reference. These are great alternative methods for updating data and implementing UI that would allow for dynamic changes within the Editor.

ndiego avatar Oct 04 '22 15:10 ndiego