I created a script to clean up some stuff in tables for a plugin (WPAmaniche) and I want to use wp_delete_post() to delete the related posts when the products are deleted.
I kept getting the following:
Fatal error: Call to undefined function wp_delete_post() in
So I decided to just activate my script as a plugin. I created a folder for it in the plugins directory and created a file with the proper header to activate it. Then I put the script in a separate file (to be called and run by a cron job). When I run the script, I still get the same error.
If I include the script in with the plugin header file, wp_delete_post() works and the script executes properly. The only problem with that is it runs every time I open my plugins admin screen in WordPress.
I know I have to tie the other file in with WordPress somehow to allow it to use wp_delete_post(), but how?
I've been googling all day. Help! Here is the script:
Thanks!
I kept getting the following:
Fatal error: Call to undefined function wp_delete_post() in
So I decided to just activate my script as a plugin. I created a folder for it in the plugins directory and created a file with the proper header to activate it. Then I put the script in a separate file (to be called and run by a cron job). When I run the script, I still get the same error.
If I include the script in with the plugin header file, wp_delete_post() works and the script executes properly. The only problem with that is it runs every time I open my plugins admin screen in WordPress.
I know I have to tie the other file in with WordPress somehow to allow it to use wp_delete_post(), but how?
I've been googling all day. Help! Here is the script:
PHP:
<?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!