Current File : /home/n742ef5/royalanteam.com/mls_bkp/rm_all.php
<?php

    ini_set('memory_limit', '1024M');
    set_time_limit(0);
    ini_set('max_execution_time', 7200);
    ini_set('display_errors', 1);
    ini_set("log_errors", 1);
    error_reporting(E_ALL);
    date_default_timezone_set('America/Toronto');
    //echo "<pre>cxcxc";

        
    $main_directory = '/home/n742ef5/royalanteam.com/';
    require_once("config/mls_config.php");
    require_once("lib/phrets.php");
    require_once("config/properties_config.php");
    require_once("/home/n742ef5/royalanteam.com/wp-load.php");
    global $wpdb;
    $website_uri = site_url();
/*
    $taxonomies = [
        'property-type',
        'offer-type',
        'city',
        'zip-code',
        'neighborhood',
        'street',
        'features',
    ];
    
    foreach ( $taxonomies as $tax ) {
    
        // Get every term (including empty ones)
        $terms = get_terms( [
            'taxonomy'   => $tax,
            'fields'     => 'ids',
            'hide_empty' => false,
        ] );
    
        if ( is_wp_error( $terms ) ) {
            error_log( sprintf( 'Could not fetch terms for taxonomy %s: %s', $tax, $terms->get_error_message() ) );
            continue;
        }
    
        foreach ( $terms as $term_id ) {
            // wp_delete_term returns WP_Error | true | false
            $deleted = wp_delete_term( $term_id, $tax );
    
            if ( is_wp_error( $deleted ) ) {
                error_log( sprintf( 'Failed deleting term %d in %s: %s', $term_id, $tax, $deleted->get_error_message() ) );
            }
        }
    }
    
    echo "✅ All specif d taxonomies have been emptied.\n";
    die;
    */    
    $timestamp_field = 'Timestamp_sql'; 
    $file_name       = "sold_property";
    $curr_resource   = "Property";
    $className       = "ResidentialProperty";
    $mls_key_field   = "Ml_num";
    
    $curr_mls_id     = 1;
    $curr_date          = date('Y-m-d H:i:s');
    $cron_tablename     = 'properties_cron_log';
    $tmp_tablename      = 'rets_property_data_resi';

    $sql_query = "SELECT ID FROM `wp_posts` where post_type='estate'";
    $all_post = $wpdb->get_results( $sql_query,ARRAY_A);
    echo "\nTotal :".count($all_post);

    if ($all_post && !empty($all_post) ) {
        foreach($all_post as $curr_post){
            // $qn_query = "SELECT post_id FROM LgJ_postmeta where meta_value ='$curr_post->Ml_num'";
            // $pre_post = $wpdb->get_results( $qn_query,ARRAY_A );
            if ($curr_post && !empty($curr_post) ) {
                //foreach($pre_post as $each_posts){
                    $post_id  = $curr_post['ID'];
                    echo "\nPost Id :".$post_id;
                    // exit;
                    // Add this id in a Temp Table
                  
                  	$media  = get_attached_media('', $post_id);
                    if (isset($media) && count($media) > 0) {
                        foreach ($media as $photo) {
                            //print_r($photo);
                            echo "\n".$main_image_path = $photo->post_content_filtered;
                            //continue;
                            error_log("Image " . $main_image_path . "<br>");
                            @unlink($main_image_path);
                            $thumbnail_image_src    = wp_get_attachment_image_src($photo->ID, 'thumbnail');
                            $thumbnail_relative_url = str_replace($website_uri.'/', $main_directory, $thumbnail_image_src['0']);
                                        
                            @unlink($thumbnail_relative_url);
                            $image_post_id   = $photo->ID;
                            $wp_delete_query = "DELETE FROM $wpdb->postmeta where post_id=$image_post_id";
                            $wpdb->query($wp_delete_query);
                            $wp_delete_query = "DELETE FROM $wpdb->posts where ID =$image_post_id";
                            $wpdb->query($wp_delete_query);
                        }
                    }
                    //continue;
                    // exit;
                    $wp_delete_query = "DELETE FROM $wpdb->postmeta where post_id=$post_id and meta_key in('_iwp_gallery','fave_property_images')";
                    $wpdb->query($wp_delete_query);
                                            
                    $remove_query_meta = "DELETE FROM $wpdb->postmeta  where post_id = $post_id ";
                    $result_remove     = $wpdb->query($remove_query_meta);
                              
                    $del_term = "DELETE FROM $wpdb->term_relationships where object_id = $post_id ";
                    $wpdb->query($del_term);
                  
                    $remove_main   = "DELETE FROM $wpdb->posts where ID = $post_id ";
                    $result_remove = $wpdb->query($remove_main);
                    
                //}
            }
            // $remove_main   = "DELETE FROM $tmp_tablename where Ml_num = '$curr_post->Ml_num' ";
            // $result_remove = $wpdb->query($remove_main);
        }
    }