0

I have some functions restricted in some hooks. The purpose of these hooks was to have consistent author name in the DB(which is a product attribute) and also update the display name. I placed this in functions.php and created hooks for these functions which I thought of implementing/triggering these hooks through WP-Crontrol plugin, apparently these functions were causing CPU Overloading (when checked in AWS instance). And I'm confused why the CPU is overloading due to these hooks?

function replace_authors_name_attributes()
{

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1
    );
    $products = new WP_Query($args);


    if ($products->have_posts()) {
        while ($products->have_posts()) {
            $products->the_post();
            $product_id = get_the_ID();
            echo "Product ID: " . $product_id . "\n";


            $product_attributes = get_post_meta($product_id, '_product_attributes', true);


            if (!empty($product_attributes)) {
                $attributes = maybe_unserialize($product_attributes);

                if (is_array($attributes) && isset($attributes['authors_name']) && isset($attributes['authors_name']['value'])) {
                    $authors_name = $attributes['authors_name']['value'];
                    echo "Author's Name: " . $authors_name . "\n<br>";


                    if ($authors_name === 'Ram' || $authors_name === 'Ramu') {
                        $new_author_name = 'Ram Rao';
                        echo "Replacing with: " . $new_author_name . "\n<br>";


                        $attributes['authors_name']['value'] = $new_author_name;
                        update_post_meta($product_id, '_product_attributes', $attributes);
                        echo "Attribute updated.\n<br>";
                    }

                }
            }
        }
    }

}
function schedule_authors_consistency() {
    if (!wp_next_scheduled('consistent_author')) {
        wp_schedule_single_event(time(), 'consistent_author');
    }
}
add_action('init', 'schedule_authors_consistency');

add_action('consistent_author', 'replace_authors_name_attributes');


function update_user_display_name($user_id, $new_display_name)
{
    global $wpdb;

    $wpdb->update(
        $wpdb->users,
        array('display_name' => $new_display_name),
        array('ID' => $user_id),
        array('%s'),
        array('%d')
    );
}
function update_authors_display_name() {

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1
);
$products = new WP_Query($args);

if ($products->have_posts()) {
    while ($products->have_posts()) {
        $products->the_post();
        $product_id = get_the_ID();
        echo "Product ID: " . $product_id . "\n";


        $author_id = get_post_field('post_author', $product_id);

        if ($author_id) {
            $current_display_name = get_the_author_meta('display_name', $author_id);
            echo "Current Display Name: " . $current_display_name . "\n<br>";

            if ($current_display_name === 'Ram') {
                $new_display_name = 'Ram Rao';
                echo "Replacing with: " . $new_display_name . "\n<br>";

                update_user_display_name($author_id, $new_display_name);
                echo "Display Name updated.\n<br>";
            }
        
        }
    }
} else {
    echo "No products found.\n";
}
}

wp_reset_postdata();
function schedule_authors_display_name_assignment() {

    if (!wp_next_scheduled('update_authors_display_name_event')) {

        wp_schedule_single_event(time(), 'update_authors_display_name_event');
    }
}
add_action('init', 'schedule_authors_display_name_assignment');

add_action('update_authors_display_name_event', 'update_authors_display_name');



function get_user_id_from_authors_name($authors_name)
{
    global $wpdb;
    $user_id = $wpdb->get_var(
        $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE display_name = %s", $authors_name)
    );
    return $user_id;
}

function update_post_authors() {


$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1
);
$products = new WP_Query($args);


if ($products->have_posts()) {
    while ($products->have_posts()) {
        $products->the_post();
        $product_id = get_the_ID();
        echo "Product ID: " . $product_id . "\n";


        $product_attributes = get_post_meta($product_id, '_product_attributes', true);


        if (!empty($product_attributes)) {
            $attributes = maybe_unserialize($product_attributes);

            if (is_array($attributes) && isset($attributes['authors_name']) && isset($attributes['authors_name']['value'])) {
                $authors_name = $attributes['authors_name']['value'];
                echo "Author's Name: " . $authors_name . "\n<br>";


                $user_id = get_user_id_from_authors_name($authors_name);

                if ($user_id) {
                    echo "User ID: " . $user_id . "\n<br>";


                    wp_update_post(array('ID' => $product_id, 'post_author' => $user_id));
                    echo "Post Author updated.\n<br>";
                } else {
                    echo "User ID not found for the given author's name.\n<br>";
                }
            }
        }
    }
} else {
    echo "No products found.\n";
}
}
wp_reset_postdata();

function schedule_update_post_authors_event() {

    if (!wp_next_scheduled('update_post_authors_event')) {

        wp_schedule_single_event(time(), 'update_post_authors_event');
    }
}
add_action('init', 'schedule_update_post_authors_event');

add_action('update_post_authors_event', 'update_post_authors');

I know that the function which updates the display_name is a mistake, instead of iterating through all the products, I should have just updated the wp_users table.

These hooks were not triggered or executed from wp-crontrol. Why is the CPU overloading? If I know the possible reason. I may prevent this from happening in the future. please do shoot me any error in my question!! Thanks

These hooks weren't even executed and CPU is overloading by their presence. Not sure why its happening?

1 Answer 1

0

This code is causing your replace_authors_name_attributes() function to run every time a page is visited on your site, so yes, your hooks are running:

schedule_authors_consistency() {
    if (!wp_next_scheduled('consistent_author')) {
        wp_schedule_single_event(time(), 'consistent_author');
    }
}
add_action('init', 'schedule_authors_consistency');

Since you do this three times and your code queries every product each time this is going to absolutely hammer your server.

If you only want the code to run once you need to save something in the database telling you that the code has already run, and then check whether that exists before running any code:

schedule_authors_consistency() {
    if (!wp_next_scheduled('consistent_author') && !get_option('consistent_author_has_run')) {
        wp_schedule_single_event(time(), 'consistent_author');
        update_option('consistent_author_has_run', '1');
    }
}
add_action('init', 'schedule_authors_consistency');
3
  • Thanks a lot!! This just simplifies everything Sep 11 at 11:36
  • I have a doubt!! Why is it running when I have created hooks for it. I'm not able to see what mistake I have made. Please help me out... thanks! Sep 11 at 11:47
  • I don't understand your question sorry. What do you mean you "created hooks for it"? On init your code schedules the consistent_author action to run immediately if there is not already a scheduled event. Each time it runs it clears the schedule so it will re-schedule it every time. Sep 11 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.