WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.58k stars 4.23k forks source link

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

Closed TachafineAittoudaWebpick closed 2 years ago

TachafineAittoudaWebpick commented 2 years ago

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 :

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

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

mrfoxtalbot commented 2 years ago

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!

kathrynwp commented 2 years ago

@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!

ndiego commented 2 years ago

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.