Monday, January 9, 2023
HomeWordPress Developmentcustomized subject - Can operating a WP-Cron to update_post_meta trigger efficiency points?

customized subject – Can operating a WP-Cron to update_post_meta trigger efficiency points?


I am counting put up views on a customized subject inside wp_postmeta, the replace occurs every time a person interacts with the put up.
I used to be studying at this response Will a big postmeta desk gradual a web site down?
particularly:

You shouldn’t write to the DB on entrance finish requests” is the eleventh
commandment. Your efficiency will deteriorate because of the writes
locking down the desk far more than the affect the scale of the desk
could have.

It’s best to most likely write such knowledge in recordsdata as a substitute of DB, or at
least use a distinct desk.

So the thought is to build up these views on a brand new desk and replace the wp_postmeta with a WP-Cron operate each hour or so. My query is: would this be dangerous for efficiency? Ought to I do it in a different way? or is it OK? I suppose it is going to be about 500-1000 rows every run.
Code:

international $wpdb;

$outcomes = $wpdb->get_results( "SELECT * FROM total_views", OBJECT );

foreach($outcomes as $key=>$worth){

    //for brand new posts with out meta
    $old_count = 0;
    if ( metadata_exists( 'put up', $value->post_id, 'post_views' ) ) {
        $old_count = get_post_meta( $value->post_id, 'post_views', true );
    }
    new_count = $value->post_views;
    $total_count = $old_count + $new_count;
    update_post_meta( $value->post_id, 'post_views', $total_count );
    }
    
//finished. empty desk
$wpdb->question('TRUNCATE TABLE total_views');

I ought to add I am on retaining wp_postmeta up to date as a result of different web site performance relies on it. Else I’d simply stick with the brand new desk.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments