• alexborras

    (@alexborras)


    When “Update” is pressed in a post, does wp_update_post run? It seems not. On a web page I use Polylang. I change the language of a post using pll_set_post_language and then call wp_update_post.

    The post assumes the category in the correct language but does not update the tags in the new language.

    However, if afterwards I only press “Update” from the list of posts or editing the post, then the tags are updated correctly and automatically.

    So something more than wp_update_post has to be done after pressing “Update” right?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If using the block editor, the update is done through the API. It possibly could go directly to wp_insert_post(), which despite its name, also does updates. If you look at the source code for wp_update_post(), it does some minor data juggling before calling wp_insert_post() itself.

    If you want to set taxonomy terms for a post, you could use wp_set_post_terms().

    Thread Starter alexborras

    (@alexborras)

    Thanks, but still not working. I have tried:

    • wp_insert_post( $my_post )
    • wp_insert_post( $my_post ) AND wp_set_post_terms ($row->ID);
    • wp_insert_post( $my_post ) AND wp_set_post_terms( $row->ID,”, ‘post_tag’, true );
    • and neither update the tags.

      The category continues to update but the post tags remain those of the previous language. So we still don’t know what exactly the “Update” button does.

    Moderator bcworkz

    (@bcworkz)

    On submit using the block editor, edit form data is POSTed to the /posts API endpoint as JSON data. Assigned tag IDs are sent under the key “tags”. The WP_REST_Posts_Controller’s update_item() method is called. It in fact does call wp_update_post() after decoding the JSON data and constructing a PHP data array. As I mentioned earlier, after a bit more data juggling, wp_insert_post() is called. After updating the posts table, the function then calls wp_set_post_tags() to assign any tag terms to the post.

    Assuming your tags are the default “post_tag” taxonomy and not some similar custom taxonomy, the usual reason for failure to update is your theme or some plugin is interfering. If you switch to twentytwenty theme and deactivate all plugins, you’ll likely find that assigned tags are properly updated. Restore your normal configuration, one module at a time. When tags again fail to update, the last activated module is the cause.

    Thread Starter alexborras

    (@alexborras)

    It’s not very clean, but it works

    wp_update_post( $my_post );
     $tags = wp_get_post_tags($row->ID);
     foreach ( $tags as $tag ){
           wp_delete_object_term_relationships( $row->ID, $tag->name );
           $tag_id_lang = pll_get_term( $tag->term_id , "es" );
           $tags_lang = array( $tag_id_lang );
           wp_set_post_terms($row->ID, $tags_lang, 'post_tag', true );                 
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘A curiosity with wp_update_post() function’ is closed to new replies.