Skip to content

The taxonomy panel post_tag do not refresh with programmatical update #15147

Closed

Description

@herewithme

Describe the bug

With WP 5.1.1 and Gutenberg plugin 5.5.0

The taxonomy panel post_tag do not refresh with programmatical update for a new relationship with a term

To reproduce

I have 2 tags with the term_id 3 & 4
I edit an article that is already linked to the term 3

If i call this function

wp.data.dispatch('core/editor').editPost({tags: [4]})

The panel does not update.

If I delete the term 3 by hand, via my browser
and i call this function

wp.data.dispatch('core/editor').editPost({tags: [3]})

The panel is updated.

In both cases, the link between the article and the terms are well recorded in the database.

Expected behavior

The update of the tags via the API must be reflected in the user interface for a new as an existing tag.

Desktop (please complete the following information):

  • OS: Mac
  • Browser Chrome
  • Version 73

Activity

  1. herewithme commented on Apr 24, 2019

    @herewithme
    Author

    The behavior is OK, if the term appeared within autocompletion (even if not clicked), following the AJAX call on the REST API!

  2. added
    [Type] Bug
    [Feature] Document Settings
    REST API Interaction
    on Apr 25, 2019
  3. Mamaduka commented on Jun 17, 2021

    @Mamaduka
    Member

    I'm able to reproduce the issue.

    The FlatTermSelector fetches term objects on componentDidMount. When post tags are updated terms programmatically new term object isn't fetched.

    If you close/open the "Tags" panel new term should be visible since component remounts.

  4. lear2012 commented on Jun 18, 2021

    @lear2012

    I experienced the same issue.
    wp.data.dispatch('core/editor').editPost({tags: [3]}) invoked in component class, it doesn't work as expected.

  5. harisrozak commented on Nov 17, 2021

    @harisrozak

    I'm able to reproduce the issue.

    The FlatTermSelector fetches term objects on componentDidMount. When post tags are updated terms programmatically new term object isn't fetched.

    If you close/open the "Tags" panel new term should be visible since component remounts.

    Nice catch!, so for now, we can programmatically toggle the "Tags" panel to achieve the right behavior, maybe something like this:

    var tag_id              = 23;
    var is_tax_panel_opened = wp.data.select( 'core/edit-post' ).isEditorPanelOpened( 'taxonomy-panel-tags' );
    var selected_term_ids   = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'tags' );
    
    // set terms
    if ( ! selected_term_ids.includes( tag_id ) ) {
    	selected_term_ids.push( tag_id );
    	wp.data.dispatch( 'core/editor' ).editPost( { 'tags': selected_term_ids } );
    
    	// close and re-open the panel to reload the term data
    	if ( is_tax_panel_opened ) {
    		wp.data.dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-tags' );
    		wp.data.dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-tags' );
    	}			
    }
    
  6. Mamaduka commented on Nov 18, 2021

    @Mamaduka
    Member

    Sorry forgot to post an update here. The issue should be fixed via #33459 and available in Gutenberg 11.9.

  7. edufolios-matt commented on Dec 8, 2021

    @edufolios-matt

    @Mamaduka - I'm currently using the approach by @harisrozak above. This updated the UI and the Redux store, yet if I save the Post (which I currently have to enable through updating a faux private metafield, otherwise the Update button doesn't enable), the tags / categories aren't saved, the UI reloads with the tags / categories cleared of any selection applied.

    Is there a mechanism to mark tags / categories "dirty" that I've missed maybe?

  8. harisrozak commented on Dec 13, 2021

    @harisrozak

    @edufolios-matt Just in case you haven't find the solution, you can try to create a new array to store the term ids instead of using the selected_term_ids. Please check my gist here: https://gist.github.com/harisrozak/64764a84cd4b3bd7845084572850ba78

  9. edufolios-matt commented on Dec 13, 2021

    @edufolios-matt

    @harisrozak thanks for that. Are you thinking that is so the array will be seen as totally new data and saved differently?

  10. harisrozak commented on Dec 13, 2021

    @harisrozak

    @edufolios-matt Yes, I don't know either why it's not working when it using the selected_term_ids, maybe it's not an array, or I did wrong with the adding the term_id into it.

  11. edufolios-matt commented on Dec 13, 2021

    @edufolios-matt

    @harisrozak - you legend!! That worked!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    REST API Interaction[Feature] Document Settings[Type] Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions