• Resolved fredhead


    I have 1100 posts that have a single image. The image links are broken. To fix them, I have to save each post manually — open the post edit page and then click the Publish button — in order to reset the image link.

    I’m wondering if clicking the Publish button calls a function that processes the page content in addition to updating the status. If so, I could programmatically call the function for each of my 1100 posts.

    It looks like wp_update_post() might do the trick? My question is whether or not that function will process the page content or only update the publication status?

    Processing the page content, for example, updating any image attachments (using ACF in this case) appears to solve the problem by resetting the link from an attachment ID to an image URL.

    Appreciate any insight on how WordPress processes posts, with function(s) and what actually happens with code when a post is published.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Where is the image? In the content or is it the feature image? I wonder why the simple saving already solves the problem.

    What https://developer.wordpress.org/reference/functions/wp_update_post/ does you can read in the manual. It only updates what is passed to it via $postarray.

    Thread Starter fredhead


    Sorry to be unclear. Using the ACF plugin, I have a top story image element assigned to posts. So there’s a panel on the edit post page where I can upload an image and type or copy/paste a source URL.

    in trading emails with the ACF plugin support team, a bug displays the ACF image ID, not the image URL. Opening a post then publishing updating the post corrects the problem.

    I’m looking to see if I can automate that publish update process, to avoid having to open 1100 posts to then click the Publish button.

    I’m hoping the wp_update_post function processes all elements in a post, including the ACF bits. I’m asking here if someone who knows the WP code can tell me what exactly happens when you click the publish button on a post page.

    Also, if it matters, I’m not using Gutenberg on the site. Only the latest underscores template and the classic text editor plugin.

    Thanks for any insights.

    Moderator bcworkz


    The Publish/Update button submits data to /wp-admin/post.php, which processes the submitted data and eventually calls wp_update_post().

    In what way are the links broken? Do you know where in the DB these links are actually stored? If they are from an ACF field, they are likely in wp_postmeta, but you should confirm this.

    Depending on how the links are broken, there might be an easy way to repair them, assuming the path itself is saved and not a reference like attachment ID. You may be able to use the Better Search and Replace plugin to patch all the links in one go if the situation is amenable to such a process.

    Compose your search string carefully to avoid inadvertently altering data that shouldn’t be altered. Verify by using the “Dry Run” feature. Also make a DB backup before making any actual changes to the DB.

    Thread Starter fredhead


    Thanks for the publishing process details at the code level.

    From working with ACF support, the problem is that the database lookup for the field ID grabs the incorrect ID. The field ID is used to retrieve an image URL that is created and stored in the post edit page. Manually opening an old post and then clicking the Publish button updates the image URL field ID to the correct one.

    The ACF field is referenced and stored in a variable in a post template to place the URL in an src= image link code. Processing the variable to convert it to a field ID and then use that ID to retrieve the image URL currently pulls the wrong ID and outputs the wrong ID instead of the image URL.

    It’s a problem with updating and assigning the correct image field ID somewhere in the database so that it can be used to retrieve the image URL.

    Hopefully that’s more clear? For me, it doesn’t really matter how this happened. I’m more interested in possible programmatic ways to update my posts.

    • This reply was modified 1 year, 3 months ago by fredhead.

    Have you looked at the examples in the manual above? There you can see how you can generally trigger an update on a post via PHP.

    Very simple example for updating a single post with ID 42:

    $post = get_post( 42, ARRAY_A );
    wp_update_post( $post );

    You could now use https://developer.wordpress.org/reference/functions/get_posts/ to query all posts relevant to you and loop through them to perform an update. I would do the whole thing as a WP CLI command because then there are no timeouts. You can also try it via browser call, but it depends on your hosting, whether this runs cleanly and no timeout occurs.

    Make backups before!

    Moderator bcworkz


    It’s a problem with updating and assigning the correct image field ID somewhere in the database

    You should be able to determine where in the database by poking around in the DB via the phpMyAdmin app. The first place to look is wp_postmeta. Search for a post ID which belongs to a post you know has the wrong image ID, but you know what that is. The wrong image ID should be present in the search results somewhere. It could possibly be within a serialized array, which can be rather tricky to decipher on sight.

    Will correcting this ID resolve the issue, or does post content also need to be corrected? Most importantly, how would an automated script know what the correct data should be for any given post if it’s currently incorrect? If that can be known, then an automated solution is certainly possible.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to Call Publish Function?’ is closed to new replies.