function roxxi_add_real_estate_listing_schema() { if (!is_singular('property')) return; global $post; $property_id = get_the_ID(); // Houzez data $address = houzez_get_listing_data('property_map_address'); $locality = houzez_taxonomy_simple('property_city'); $region = houzez_taxonomy_simple('property_state') ?: 'CA'; $postal_code = get_post_meta($property_id, 'fave_property_zip', true); $price = houzez_get_listing_data('property_price'); $price_clean = floatval(preg_replace('/[^0-9.]/', '', $price)); $bedrooms = houzez_get_listing_data('property_bedrooms'); $bathrooms = houzez_get_listing_data('property_bathrooms'); $rooms = houzez_get_listing_data('property_rooms'); $size = houzez_get_listing_data('property_size'); $year_built = get_post_meta($property_id, 'fave_property_year', true); $number_of_units = get_post_meta($property_id, 'fave_property_units', true); // Amenities from property_feature taxonomy $feature_terms = get_the_terms($property_id, 'property_feature'); $amenities = []; if (!is_wp_error($feature_terms) && !empty($feature_terms)) { foreach ($feature_terms as $term) { $amenities[] = [ "@type" => "LocationFeatureSpecification", "name" => $term->name, "value" => true ]; } } $listing_date = get_the_date('Y-m-d', $post->ID); $property_type = houzez_taxonomy_simple('property_type'); $country = houzez_taxonomy_simple('property_country') ?: 'US'; $listing_id = get_post_meta($property_id, 'fave_mls_id', true); $property_type_key = strtolower(str_replace([' ', '-'], '', $property_type)); $schema_type = match ($property_type_key) { 'boatslip' => 'BoatSlip', 'cabin' => 'House', 'condominium' => 'Apartment', 'singlefamilyresidence' => 'House', 'townhouse' => 'House', default => 'Property' }; $agent_name = get_post_meta($property_id, 'fave_agent_display_name', true); $agent_email = get_post_meta($property_id, 'fave_agent_email', true); $agent_phone = get_post_meta($property_id, 'fave_agent_mobile', true); $images = []; if (has_post_thumbnail($post->ID)) { $images[] = get_the_post_thumbnail_url($post->ID, 'full'); } $gallery_ids = get_post_meta($post->ID, 'fave_property_images', true); if (!empty($gallery_ids) && is_array($gallery_ids)) { foreach ($gallery_ids as $img_id) { $images[] = wp_get_attachment_url($img_id); } } if (!$address || !$locality || !$region || !$postal_code || !$price_clean) return; $property_schema = [ "@context" => "https://schema.org", "@type" => "Product", "name" => get_the_title(), "url" => get_permalink(), "image" => $images, "description" => wp_strip_all_tags(get_the_excerpt()), "offers" => [ "@type" => "Offer", "price" => $price_clean, "priceCurrency" => "USD", "validFrom" => $listing_date, "eligibleRegion" => ["@type" => "Place", "name" => $locality . ', ' . $region] ], "isRelatedTo" => [ "@type" => $schema_type, "identifier" => !empty($listing_id) ? ["@type" => "PropertyValue", "name" => "MLS Listing ID", "value" => $listing_id] : null, "numberOfRooms" => (int)$rooms, "numberOfBedrooms" => (int)$bedrooms, "numberOfBathroomsTotal" => (int)$bathrooms, "floorSize" => ["@type" => "QuantitativeValue", "value" => (int)$size, "unitCode" => "SQF"], "yearBuilt" => $year_built ?: null, "numberOfUnits" => $number_of_units ?: null, "address" => ["@type" => "PostalAddress", "streetAddress" => $address, "addressLocality" => $locality, "addressRegion" => $region, "postalCode" => $postal_code, "addressCountry" => $country], "mainEntityOfPage" => get_permalink(), "amenityFeature" => $amenities ] ]; $agent_schema = !empty($agent_name) || !empty($agent_email) || !empty($agent_phone) ? [ "@context" => "https://schema.org", "@type" => "RealEstateAgent", "name" => $agent_name ?: 'Listing Agent', "email" => $agent_email ?: 'info@example.com', "telephone" => $agent_phone ?: '+1-000-000-0000', "url" => get_permalink() ] : null; function roxxi_clean_schema($array) { foreach ($array as $key => &$value) { if (is_array($value)) { $value = roxxi_clean_schema($value); } if ($value === null || $value === [] || $value === '') { unset($array[$key]); } } return $array; } $property_schema = roxxi_clean_schema($property_schema); $agent_schema = roxxi_clean_schema($agent_schema); echo ''; if (!empty($agent_schema)) { echo ''; } } add_action('wp_head', 'roxxi_add_real_estate_listing_schema'); // EOF
Marketing Director at RoxxiStudios™
Compare listings
Compare