Forums

Need script to hook when script is loaded via cron so I can use wp_delete_post (2 posts)

  1. jondoe0069
    Member
    Posted 2 years ago #

    Hello,

    I have created a script to clean up some products in the database tables of one of my plugins. When this script runs, I need it to delete the corresponding WordPress posts associated with the deleted products. When I added a plugin header to the script and activated it in wordpress, it worked fine, but it constantly ran when I was logged into the admin interface so I split it into two files.

    What I have now is:

    script-name-folder/
    script-plugin-header.php
    script.php

    script.php doesn't have any hooks in it, so wp_delete_post fails. Is there such a thing as a hook that I can use to allow wp_delete_post to run only when the script is loaded? I would like to simply run the script every night using a cron job. Below is the portion of the script that uses the wp_delete_post function and below that is the full script (Please Help!!):

    $q="select distinct post_id from wp_amazon_products where (product_image='' or product_image is null)
        or (price=0 or price is null)  or datediff(curdate(),updated)>30";
    $rs=mysql_query($q);
    while ($r=mysql_fetch_row($rs)) {
     echo "\n<br>Delete post: $r[0]";
     wp_delete_post($r[0]);
    }
    <?php
    $host="localhost";
    $user="";
    $password="";
    $database="";
    mysql_connect($host,$user,$password) or die ("Can not connect to mysql server");
    mysql_select_db($database) or die("Can not select working database");
    
    $q="select distinct post_id from wp_amazon_products where (product_image='' or product_image is null)
        or (price=0 or price is null)  or datediff(curdate(),updated)>30";
    $rs=mysql_query($q);
    while ($r=mysql_fetch_row($rs)) {
     echo "\n<br>Delete post: $r[0]";
     wp_delete_post($r[0]);
    } 
    
    $q="delete from wp_amazon_products where product_image='' or product_image is null";
    mysql_query($q) or die(mysql_error());
    echo "\n<br>image deleted: ".mysql_affected_rows();
    
    $q="delete from wp_amazon_products where price=0 or price is null";
    mysql_query($q) or die(mysql_error());
    echo "\n<br>price deleted: ".mysql_affected_rows();
    
    $q="delete from wp_amazon_products where datediff(curdate(),updated)>30 ";
    mysql_query($q) or die(mysql_error());
    echo "\n<br>date deleted: ".mysql_affected_rows();
    
    $q="delete from wp_amazon_products_attributes where product_id not in (select id from wp_amazon_products) ";
    mysql_query($q) or die(mysql_error());
    echo "\n<br>attributes deleted: ".mysql_affected_rows();
    
    $q="delete from wp_amazon_products_related where product_id not in (select id from wp_amazon_products) ";
    mysql_query($q) or die(mysql_error());
    echo "\n<br>related deleted: ".mysql_affected_rows();
    ?>

    Thanks!

  2. jondoe0069
    Member
    Posted 2 years ago #

    Fixed - it didn't have to be a plugin. To use the global functions in WP, I just had to add the following:

    include('../wp-config.php');
    global $wpdb, $table_prefix;

    It might be ugly, but it worked.

Topic Closed

This topic has been closed to new replies.

About this Topic