Created
July 15, 2025 18:08
-
-
Save wolffe/9ede41b978b5286003dbffb69ad83ac2 to your computer and use it in GitHub Desktop.
Export properties from the Houzez theme in JSON format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Property Drive - Houzez Exporter | |
* Plugin URI: https://www.4property.com/ | |
* Description: - | |
* Version: 1.0.0 | |
* Author: 4Property | |
* Author URI: https://www.4property.com/ | |
* License: GNU General Public License v3 or later | |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
// require_once( dirname( __FILE__ ) . '/wp-load.php' ); | |
function export_properties_data() { | |
if ( isset( $_GET['export']) ) { | |
error_reporting( E_ALL ); | |
ini_set( 'display_errors', 1 ); | |
$args = array( | |
'post_type' => 'property', | |
'posts_per_page' => -1, // Retrieve all properties, you can adjust this number if needed | |
); | |
$properties = new WP_Query( $args ); | |
$property_data = array(); | |
if ( $properties->have_posts() ) { | |
while ( $properties->have_posts() ) { | |
$properties->the_post(); | |
$property_id = get_the_ID(); | |
$property_meta = get_post_meta( $property_id ); // Retrieve all meta fields for the property | |
$image_ids = $property_meta['fave_property_images']; | |
$image_urls = []; // Array to store image URLs | |
if ($image_ids) { | |
foreach ($image_ids as $image_id) { | |
$image_urls[] = wp_get_attachment_url( (int) $image_id ); // Retrieve the URL of each image | |
} | |
} | |
$taxonomy_terms = array(); | |
$taxonomies = get_object_taxonomies('property'); // Retrieve all taxonomies associated with the 'property' post type | |
foreach ($taxonomies as $taxonomy) { | |
$terms = get_the_terms($property_id, $taxonomy); | |
if ($terms && !is_wp_error($terms)) { | |
$taxonomy_terms[$taxonomy] = array(); | |
foreach ($terms as $term) { | |
$taxonomy_terms[$taxonomy][] = $term->name; | |
} | |
} | |
} | |
$property_data[] = array( | |
'ID' => $property_id, | |
'post_title' => get_the_title(), | |
'post_description' => get_the_content(), | |
'post_excerpt' => get_the_excerpt(), | |
'meta' => $property_meta, | |
'image_urls' => $image_urls, | |
'creation_date' => get_the_date('Y-m-d'), // Get the creation date of the property | |
'modification_date' => get_the_modified_date('Y-m-d'), // Get the modification date of the property | |
'taxonomies' => $taxonomy_terms, // Include the taxonomies and their associated terms | |
); | |
} | |
wp_reset_postdata(); | |
} | |
echo json_encode( $property_data ); | |
} elseif ( isset( $_GET['exportsql'] ) ) { | |
global $wpdb; | |
// Define batch size for fetching posts | |
$batch_size = 1000; | |
$offset = 0; | |
// Output as JSON stream | |
echo '['; | |
$first_batch = true; | |
do { | |
// Step 1: Query to get basic post data | |
$query = $wpdb->prepare(" | |
SELECT | |
p.ID, | |
p.post_title, | |
p.post_content AS post_description, | |
p.post_excerpt, | |
p.post_date AS creation_date, | |
p.post_modified AS modification_date | |
FROM {$wpdb->posts} p | |
WHERE p.post_type = 'property' | |
LIMIT %d OFFSET %d | |
", $batch_size, $offset); | |
$properties = $wpdb->get_results( $query ); | |
if ( empty( $properties ) ) { | |
break; // No more properties to fetch, exit the loop | |
} | |
foreach ( $properties as $property ) { | |
// Step 2: Fetch metadata for each post | |
$property_meta = $wpdb->get_results( | |
$wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $property->ID ), | |
OBJECT_K | |
); | |
$meta = array(); | |
foreach ( $property_meta as $meta_key => $meta_value ) { | |
$meta[$meta_key] = maybe_unserialize( $meta_value->meta_value ); | |
} | |
// Step 3: Fetch image URLs from meta key 'fave_property_images' | |
$image_ids = isset( $meta['fave_property_images'] ) ? $meta['fave_property_images'] : array(); | |
$image_urls = array(); | |
if ( !empty( $image_ids ) ) { | |
foreach ( $image_ids as $image_id ) { | |
$image_urls[] = wp_get_attachment_url( (int) $image_id ); | |
} | |
} | |
// Step 4: Fetch taxonomy terms for the property | |
$taxonomy_terms = array(); | |
$taxonomies = get_object_taxonomies( 'property' ); // Retrieve all taxonomies for the 'property' post type | |
foreach ( $taxonomies as $taxonomy ) { | |
$terms = get_the_terms( $property->ID, $taxonomy ); | |
if ( $terms && ! is_wp_error( $terms ) ) { | |
$taxonomy_terms[$taxonomy] = array(); | |
foreach ( $terms as $term ) { | |
$taxonomy_terms[$taxonomy][] = $term->name; | |
} | |
} | |
} | |
// Step 5: Prepare property data | |
$property_data = array( | |
'ID' => $property->ID, | |
'post_title' => $property->post_title, | |
'post_description' => $property->post_description, | |
'post_excerpt' => $property->post_excerpt, | |
'meta' => $meta, | |
'image_urls' => $image_urls, | |
'creation_date' => $property->creation_date, | |
'modification_date' => $property->modification_date, | |
'taxonomies' => $taxonomy_terms, | |
); | |
// Output the property data (batch JSON encoding) | |
if ( !$first_batch ) { | |
echo ','; // Separate JSON objects with a comma | |
} else { | |
$first_batch = false; | |
} | |
echo json_encode( $property_data ); | |
} | |
// Increase the offset for the next batch | |
$offset += $batch_size; | |
// Free memory between batches | |
gc_collect_cycles(); | |
} while ( true ); | |
echo ']'; | |
/* | |
$query = " | |
SELECT | |
p.ID, | |
p.post_title, | |
p.post_content AS post_description, | |
p.post_excerpt, | |
p.post_date AS creation_date, | |
p.post_modified AS modification_date, | |
GROUP_CONCAT(pm.meta_key, ':', pm.meta_value SEPARATOR '|') AS meta, | |
GROUP_CONCAT(t.name SEPARATOR ',') AS taxonomies, | |
GROUP_CONCAT(IFNULL(a.guid, '') SEPARATOR ',') AS image_urls | |
FROM {$wpdb->posts} p | |
LEFT JOIN {$wpdb->postmeta} pm | |
ON p.ID = pm.post_id | |
LEFT JOIN {$wpdb->term_relationships} tr | |
ON p.ID = tr.object_id | |
LEFT JOIN {$wpdb->term_taxonomy} tt | |
ON tr.term_taxonomy_id = tt.term_taxonomy_id | |
LEFT JOIN {$wpdb->terms} t | |
ON tt.term_id = t.term_id | |
LEFT JOIN {$wpdb->postmeta} pm_images | |
ON p.ID = pm_images.post_id AND pm_images.meta_key = 'fave_property_images' | |
LEFT JOIN {$wpdb->posts} a | |
ON a.ID = pm_images.meta_value | |
WHERE p.post_type = 'property' | |
GROUP BY p.ID | |
"; | |
$results = $wpdb->get_results( $query ); | |
$property_data = array(); | |
if ( $results ) { | |
foreach ( $results as $property ) { | |
$meta_pairs = explode('|', $property->meta); | |
$meta = array(); | |
foreach ( $meta_pairs as $meta_pair ) { | |
list($key, $value) = explode(':', $meta_pair); | |
$meta[$key] = $value; | |
} | |
$property_data[] = array( | |
'ID' => $property->ID, | |
'post_title' => $property->post_title, | |
'post_description' => $property->post_description, | |
'post_excerpt' => $property->post_excerpt, | |
'meta' => $meta, | |
'image_urls' => explode(',', $property->image_urls), | |
'creation_date' => $property->creation_date, | |
'modification_date' => $property->modification_date, | |
'taxonomies' => explode(',', $property->taxonomies), | |
); | |
} | |
} | |
echo json_encode( $property_data ); | |
/**/ | |
} | |
} | |
add_action( 'init', 'export_properties_data' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment