• WildBil2Me

    (@wildbil2me)


    I have a custom post type which occasionally needs to be updated using information from an external source.

    As of right now, I have a function that does the work properly but it requires that the post be re-loaded in order for the correct data to be displayed.

    This is accomplished using some php I’ve added to my single.php file right before the Loop.

    simplified example:
    <?php if(function_exists('price_updater')) { price_updater(); } ?>

    ‘price_updater’ uses ‘wp_update_post’ to update the content and a meta_field. These definitely get updated but I get the previous version on the first pass. If I hit the reload page it shows the updated version.

    So the question is, how do I set this up to avoid refreshing the page?

    I’m guessing using ‘add_action’ or ‘add_filter’ but I’ve had no success so far – the handful I’ve tried either have the same problem or do nothing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Are you using a a caching plugin on your site? If so exclude that post (those posts) from caching.

    Thread Starter WildBil2Me

    (@wildbil2me)

    I have no caching plugins running. Right now it’s a vanilla WordPress install.

    I’ve also confirmed that it’s not the browser cache. Devices that have never visited the site still have to reload the page to see the updates.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’m not sure but I don’t think updating a post within single.php is the solution, because by the time you get to that part, the post may have already been read. Try using a filter on pre_get_posts to update the post before the query takes place.

    Thread Starter WildBil2Me

    (@wildbil2me)

    Thank you for your response!

    I tried ‘pre_get_posts’ which required that I change up how I got the postID. Unfortunately it ran forever without doing anything but it got me moving in the right direction.

    Ultimately I landed on ‘parse_request’. I followed it up with a check to make sure I was in my custom post type and not on the admin panel. It appears to be working great now.

    Again, thanks for the suggestion!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying a post correctly after wp_update_post’ is closed to new replies.