如何通过Property Hive Hook获取XML值及TRANS_TYPE_ID并设置后台状态?
解决Property Hive获取XML<TRANS_TYPE_ID>并设置交易状态的方案
一、批量导入场景:利用导入钩子拦截XML数据
Property Hive在从XML/房源Feed导入房源时,会触发核心动作钩子,无需修改插件代码即可拦截数据并调整状态:
- 使用
propertyhive_before_save_property_from_feed钩子(房源存入数据库前触发),直接修改待保存的房源参数:
add_action( 'propertyhive_before_save_property_from_feed', 'ph_extract_trans_type_set_status', 10, 2 ); function ph_extract_trans_type_set_status( $property_data, $feed_item ) { // 根据你的XML结构提取<TRANS_TYPE_ID>,此处假设feed_item为SimpleXMLElement对象 if ( isset( $feed_item->TRANS_TYPE_ID ) ) { $trans_type_id = trim( (string) $feed_item->TRANS_TYPE_ID ); // 映射ID到Property Hive内置状态('for-sale'=购买,'to-rent'=出租) switch ( $trans_type_id ) { case '1': $property_data['status'] = 'for-sale'; break; case '2': $property_data['status'] = 'to-rent'; break; // 可根据实际业务ID扩展更多规则 } } return $property_data; }
二、手动录入场景:利用房源保存钩子处理
如果是手动添加房源时需从XML字段获取TRANS_TYPE_ID,使用propertyhive_before_save_property钩子:
add_action( 'propertyhive_before_save_property', 'ph_set_status_from_manual_xml' ); function ph_set_status_from_manual_xml( $property_id ) { // 假设XML内容通过自定义表单字段提交,此处以POST参数为例 if ( isset( $_POST['custom_xml_content'] ) && !empty( $_POST['custom_xml_content'] ) ) { $xml = simplexml_load_string( $_POST['custom_xml_content'] ); if ( $xml && isset( $xml->TRANS_TYPE_ID ) ) { $trans_type_id = trim( (string) $xml->TRANS_TYPE_ID ); $target_status = ( $trans_type_id == '1' ) ? 'for-sale' : 'to-rent'; // 更新房源状态元数据 update_post_meta( $property_id, '_status', $target_status ); } } }
三、调试验证技巧
若不确定钩子是否触发或XML节点是否正确,可添加日志输出:
// 在回调函数内插入 error_log( 'Property Hive Debug: TRANS_TYPE_ID = ' . ( isset( $feed_item->TRANS_TYPE_ID ) ? (string)$feed_item->TRANS_TYPE_ID : '未找到' ) );
开启WordPress的WP_DEBUG后,可在wp-content/debug.log中查看日志,确认数据提取是否正常。
内容的提问来源于stack exchange,提问作者Nika Zerekidze
相关产品推荐
相关产品推荐

