• hello
    for our site we are creating an alphabetical index.
    To achieve this we enter a (mostly) lastname in the custom field keyword, of which the first letter is stored in an additional field called alphabetical_letter.

    We have created a script that does this for any article we update manually. However since we have a large database we have no desire to do this for each single post… is there a way to achieve this for the whole database and make this script below run once for all posts ?

    we have been trying and experimenting but havent had a lot of succes thusfar so any help is greatly appreciated.

    function my_acf_update_letter( $value, $post_id, $field  ) {
    
        $taxonomy = 'alphabetical_letter';
        $first_letter = strtoupper(substr($value, 0, 1));
    
        wp_set_post_terms( $post_id, $first_letter, $taxonomy );
    
        // return
        return $value;
    
    }
    add_filter('acf/update_value/name=keyword', 'my_acf_update_letter', 10, 3);
Viewing 4 replies - 1 through 4 (of 4 total)
  • getting the data from the db using a loop and running the function
    or SQL instructions on the db directly
    I’m not savvy enough to code this for you but shouldn’t be too complex to code in a loop on top of your function

    Easiest way I can think of to get WP to loop through all posts, is to hijack the page template mechanism, get the template from your theme that displays multiple posts, maybe the post archive template, rename it and adapt the loop to call your function, get WP to run your new page template and you will be very close to done.

    Moderator bcworkz

    (@bcworkz)

    The previous replies are on the right track, but there are some issues to iron out. I agree with Ross that a page template is a good solution, but starting with archive template code has limited utility because it relies on the main query to fetch one page’s worth of posts. You really want to fetch all posts, but doing so will cause your PHP script to time out.

    I suggest making a custom page template, but creating your own custom query and loop that excludes existing posts already with a ‘alphabetical_letter’ term assigned. Set the posts per page argument to a good quantity of posts, but not so many that the script times out. If you reload the page enough times, eventually all posts will be processed.

    You can also override the allowed script time with set_time_limit() (this is normally set in php.ini to 30 seconds). Send some occasional status messages to the browser or else it will also time out regardless of the PHP time allowed.

    I’d advise first limiting the posts processed to a small quantity until you are sure your script works infallibly. Then you can turn it loose on large chunks of data. Also make a backup of your DB before making significant changes.

    Thread Starter nessler

    (@nessler)

    thanks for your input all. will be working my way through this, probably with a lot of trial and error but at least I know where to look now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How can we run an update script on all past posts ?’ is closed to new replies.