更新不同目的地和度假村的相关房间的 ACF 字段

问题描述 投票:0回答:1

Stack Overflow 社区您好,

我正在使用 ACF(高级自定义字段)和 JetEngine 开发一个 WordPress 项目。我有一个场景,我需要更新特定度假村(“Grand Luxxe”)内和不同目的地之间共享相同名称(“主卧室”)的房间的特定 ACF 字段,即“discount_percentage”。

以下是设置的简要概述:

我使用 JetEngine 来管理自定义帖子类型:“房间”、“目的地”和“度假村”。 我使用 JetEngine 的“房间到度假村”关系字段在“房间”和“度假村”之间创建了多对多关系。 “房间”可以属于多个度假村,反之亦然。 现在,当我更新“Nuevo Vallarta | Grand Luxxe Master Bedroom”中房间的“discount_percentage”字段时,我希望此更改反映在“Nuevo Vallarta | Grand Luxxe Master Bedroom”中具有相同名称(“Master Bedroom”)的所有房间中。 Grand Luxxe' 度假村,但目的地不同。

我可以使用自定义函数或方法来实现此目的吗?我探索过JetEngine,它似乎没有内置的功能来检索相关项目。

任何指导、代码片段或替代方法将不胜感激。预先感谢!

我面临的具体挑战涉及更新各个目的地的特定度假村(“Grand Luxxe”)内具有相同名称(“主卧室”)的房间的 ACF 字段“discount_percentage”。

这是细分:

我使用 JetEngine 来管理自定义帖子类型,包括“房间”、“目的地”和“度假村”。 房间可以与多个度假村关联,反之亦然。 我的目标是实现一种机制,修改房间的“discount_percentage”字段,例如“Nuevo Vallarta | Grand Luxxe Master Bedroom”,会触发“Grand Luxxe”度假村内所有同名房间的相同更新,但位于不同的目的地。

我探索过 JetEngine,但发现没有用于检索相关项目的内置功能。如果有人遇到过类似的场景或者可以提供见解、代码片段或替代方法

php wordpress advanced-custom-fields jet-engine
1个回答
0
投票

要实现此目的,您可以挂钩 WordPress 保存帖子操作,以便在保存帖子时运行自定义代码。在您的情况下,当保存房间帖子时,您将需要在同一度假村内但在不同的目的地中查找所有其他具有相同名称的相关房间,然后更新其 ACF 字段。

以下是如何实现这一目标的大致思路。请注意,实际实施可能会有所不同,具体取决于您的设置的具体情况。

1。挂钩保存后操作

您可以使用 save_post 操作在保存帖子时运行函数。

add_action('save_post', 'update_related_rooms', 10, 3);

2。查找相关房间

在您的挂钩函数中,您必须根据房间名称和度假村找到所有相关的房间。

3.更新 ACF 字段

对于您找到的每个相关房间,您可以使用ACF的update_field()函数来更新discount_percentage字段。

这是挂钩函数的草图:

function update_related_rooms($post_id, $post, $update) {

// Check if this is a Room post type.
if ('room' !== $post->post_type) {
    return;
}

// Get the name of the Room being saved.
$room_name = get_the_title($post_id);

// Get the Resort associated with this Room.
$resort = get_the_terms($post_id, 'resort'); // Assuming 'resort' is the taxonomy. Modify as per your setup.

// If no associated resort is found, return.
if (!$resort || is_wp_error($resort)) {
    return;
}

$resort_id = $resort[0]->term_id; // If multiple resorts are associated, you might need to loop through them.

// Get the new discount_percentage value.
$discount_percentage = get_field('discount_percentage', $post_id);

// Define the query to find related rooms.
$args = array(
    'post_type' => 'room',
    'tax_query' => array(
        array(
            'taxonomy' => 'resort',
            'field' => 'term_id',
            'terms' => $resort_id,
        ),
    ),
    'post__not_in' => array($post_id), // Exclude the current post.
    'meta_query' => array(
        array(
            'key' => 'room_name', // Replace with your actual meta key for Room name.
            'value' => $room_name,
        ),
    ),
);

// Execute the query.
$related_rooms = get_posts($args);

// Loop through the related rooms and update the discount_percentage field.
foreach ($related_rooms as $room) {
    update_field('discount_percentage', $discount_percentage, $room->ID);
}

}

add_action('save_post', 'update_lated_rooms', 10, 3);

一些注意事项

根据您的实际分类和元键设置调整分类查询。 如果一个 Room 可以属于多个 Resorts,您可能需要调整逻辑来处理多个关联的 Resorts。 确保将上面示例中的占位符替换为您的实际元键、分类名称等。 执行彻底的测试以确保不会发生无限循环,并且其他帖子/类型不会受到此逻辑的影响。 代码的效率,尤其是相关房间的 WP 查询,应该根据系统中的实际帖子数量和关系来评估和优化。

© www.soinside.com 2019 - 2024. All rights reserved.