将此代码段编辑为 运行 一次
Editing this Snippet to Run One Time Only
我们有数以千计的自定义 post,我们需要使用此 PHP 片段更新 the_excerpt(在保存单个 post 时效果很好)。有没有办法调整这段代码,以便我们可以 运行 它一次(比如当我们加载页面时)然后删除它?我们正在尝试根据此代码更新每个 post 的摘录,而无需手动编辑每个 post.
// This saves the Short Description ACF field as the post excerpt.
add_action('acf/save_post', function($post_id) {
$post_type = get_post_type( $post->ID );
if ($post_type == "ultratech_product") {
$post_excerpt = get_field( 'short_description', $post_id );
if ( '' != $post_excerpt ) {
$post_excerpt = strip_shortcodes( $post_excerpt );
$post_excerpt = apply_filters('the_content', $post_excerpt);
$post_excerpt = str_replace(']]>', ']]>', $post_excerpt);
$excerpt_length = 250; // 250 words
$excerpt_more = apply_filters('excerpt_more', " " . '[...]');
$post_excerpt = wp_trim_words( $post_excerpt, $excerpt_length, $excerpt_more );
}
// Update post options array
$update_post_excerpt_args = array(
'ID' => $post_id,
'post_excerpt' => $post_excerpt,
);
wp_update_post( $update_post_excerpt_args );
}
}, 40);
首先,请务必阅读此处的所有内容。如果您对任何事情感到不舒服,我建议您联系开发人员让他们检查并 运行 代表您进行检查。确保正确更新数千个 post 应该是一个很小的代价。
其次,进行完整备份并确保可以恢复它。
第三,将该备份保存在某个地方以备将来访问,以防万一您暂时找不到此逻辑中的缺陷。
有几种方法可以做到这一点。就个人而言,我会构建一个 CLI 版本,因为我喜欢进度条和详细的状态消息,但那只是我。您还可以制作一个插件,使用查询字符串触发特殊 PHP 代码,使用魔法页面或页面模板,对于此代码,我将使用最后一个选项,页面模板。
将以下内容放入主题根目录下的新 PHP 文件中。从技术上讲,文件名并不重要,但按照惯例,我在模板前加上前缀 tpl
,因此我建议将这个新文件命名为 tpl-update.php
。将以下代码粘贴到该文件中,但请确保在代码块之后继续阅读。
<?php
/**
* Template Name: Excerpt Updater
*/
die('Remove me when ready');
// Turn on full error reporting
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Disable PHP timeouts
set_time_limit(0);
// Grab all products
$allProducts = get_posts(
[
'post_type' => 'ultratech_product',
'posts_per_page' => -1,
]
);
foreach ($allProducts as $product) {
echo 'Processing post ' . $product->ID . ';' . PHP_EOL;
// We're guaranteed to only have our products, so we don't need to check the type anymore
// Get the ACF field
$post_excerpt = get_field('short_description', $product->ID);
// If it has anything in it, apply the logic (which was untested)
if ($post_excerpt) {
$post_excerpt = strip_shortcodes($post_excerpt);
$post_excerpt = apply_filters('the_content', $post_excerpt);
$post_excerpt = str_replace(']]>', ']]>', $post_excerpt);
$excerpt_length = 250; // 250 words
$excerpt_more = apply_filters('excerpt_more', " " . '[...]');
$post_excerpt = wp_trim_words($post_excerpt, $excerpt_length, $excerpt_more);
// Only update the post if we made a change above
// Update post options array
$update_post_excerpt_args = array(
'ID' => $product->ID,
'post_excerpt' => $post_excerpt,
);
wp_update_post($update_post_excerpt_args);
}
}
echo 'Done';
在 WordPress 中,创建一个新页面并将其分配给 Excerpt Updater 模板:
.
上面的代码 明确地 有一个 die
语句作为逻辑的第一行,以便您可以测试 WordPress 是否正在使用该模板。明确地说,您可以发布和查看该页面,您将看到的只是 Remove me when ready
、没有任何工作会完成。
看到该消息后,您可以在模板代码中注释掉该行,它将处理所有内容。这可能需要一段时间,所以我尝试禁用任何 PHP 超时,但是服务器或其他东西可能会取消脚本。
但在你 运行 实时代码 之前,我还建议你将 posts_per_page
的值更改为较小的数字,例如 [=16] =] 这样你就可以确保一切正常。该代码将 echo
输出产品的 ID,以便您可以在后端检查它。
确认它按预期工作后,您可以再次将 posts_per_page
改回 -1
,这意味着“所有 post”(给定的 post类型)。
最后一件事,我并没有真正审查更新摘录本身的逻辑,我假设你已经这样做了,但快速浏览一下似乎是正确的。
好的,还有最后一件事。通常当我做这样的事情时,我会创建一个名为“摘录版本”的新 ACF 字段,默认情况下该字段为空。然后,我 运行 我的循环并说“如果摘录版本不是 v2
,请更新摘录并将版本设置为 v2
”。通过这样做,我可以保证逻辑只 运行 一次。您不必这样做,但如果您想 运行 分批处理代码(例如一次 10 个)以进行测试,这会很有帮助。如果您认为有一天您可能不得不再次更改摘录,它也会有所帮助。
我们有数以千计的自定义 post,我们需要使用此 PHP 片段更新 the_excerpt(在保存单个 post 时效果很好)。有没有办法调整这段代码,以便我们可以 运行 它一次(比如当我们加载页面时)然后删除它?我们正在尝试根据此代码更新每个 post 的摘录,而无需手动编辑每个 post.
// This saves the Short Description ACF field as the post excerpt.
add_action('acf/save_post', function($post_id) {
$post_type = get_post_type( $post->ID );
if ($post_type == "ultratech_product") {
$post_excerpt = get_field( 'short_description', $post_id );
if ( '' != $post_excerpt ) {
$post_excerpt = strip_shortcodes( $post_excerpt );
$post_excerpt = apply_filters('the_content', $post_excerpt);
$post_excerpt = str_replace(']]>', ']]>', $post_excerpt);
$excerpt_length = 250; // 250 words
$excerpt_more = apply_filters('excerpt_more', " " . '[...]');
$post_excerpt = wp_trim_words( $post_excerpt, $excerpt_length, $excerpt_more );
}
// Update post options array
$update_post_excerpt_args = array(
'ID' => $post_id,
'post_excerpt' => $post_excerpt,
);
wp_update_post( $update_post_excerpt_args );
}
}, 40);
首先,请务必阅读此处的所有内容。如果您对任何事情感到不舒服,我建议您联系开发人员让他们检查并 运行 代表您进行检查。确保正确更新数千个 post 应该是一个很小的代价。
其次,进行完整备份并确保可以恢复它。
第三,将该备份保存在某个地方以备将来访问,以防万一您暂时找不到此逻辑中的缺陷。
有几种方法可以做到这一点。就个人而言,我会构建一个 CLI 版本,因为我喜欢进度条和详细的状态消息,但那只是我。您还可以制作一个插件,使用查询字符串触发特殊 PHP 代码,使用魔法页面或页面模板,对于此代码,我将使用最后一个选项,页面模板。
将以下内容放入主题根目录下的新 PHP 文件中。从技术上讲,文件名并不重要,但按照惯例,我在模板前加上前缀 tpl
,因此我建议将这个新文件命名为 tpl-update.php
。将以下代码粘贴到该文件中,但请确保在代码块之后继续阅读。
<?php
/**
* Template Name: Excerpt Updater
*/
die('Remove me when ready');
// Turn on full error reporting
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Disable PHP timeouts
set_time_limit(0);
// Grab all products
$allProducts = get_posts(
[
'post_type' => 'ultratech_product',
'posts_per_page' => -1,
]
);
foreach ($allProducts as $product) {
echo 'Processing post ' . $product->ID . ';' . PHP_EOL;
// We're guaranteed to only have our products, so we don't need to check the type anymore
// Get the ACF field
$post_excerpt = get_field('short_description', $product->ID);
// If it has anything in it, apply the logic (which was untested)
if ($post_excerpt) {
$post_excerpt = strip_shortcodes($post_excerpt);
$post_excerpt = apply_filters('the_content', $post_excerpt);
$post_excerpt = str_replace(']]>', ']]>', $post_excerpt);
$excerpt_length = 250; // 250 words
$excerpt_more = apply_filters('excerpt_more', " " . '[...]');
$post_excerpt = wp_trim_words($post_excerpt, $excerpt_length, $excerpt_more);
// Only update the post if we made a change above
// Update post options array
$update_post_excerpt_args = array(
'ID' => $product->ID,
'post_excerpt' => $post_excerpt,
);
wp_update_post($update_post_excerpt_args);
}
}
echo 'Done';
在 WordPress 中,创建一个新页面并将其分配给 Excerpt Updater 模板:
上面的代码 明确地 有一个 die
语句作为逻辑的第一行,以便您可以测试 WordPress 是否正在使用该模板。明确地说,您可以发布和查看该页面,您将看到的只是 Remove me when ready
、没有任何工作会完成。
看到该消息后,您可以在模板代码中注释掉该行,它将处理所有内容。这可能需要一段时间,所以我尝试禁用任何 PHP 超时,但是服务器或其他东西可能会取消脚本。
但在你 运行 实时代码 之前,我还建议你将 posts_per_page
的值更改为较小的数字,例如 [=16] =] 这样你就可以确保一切正常。该代码将 echo
输出产品的 ID,以便您可以在后端检查它。
确认它按预期工作后,您可以再次将 posts_per_page
改回 -1
,这意味着“所有 post”(给定的 post类型)。
最后一件事,我并没有真正审查更新摘录本身的逻辑,我假设你已经这样做了,但快速浏览一下似乎是正确的。
好的,还有最后一件事。通常当我做这样的事情时,我会创建一个名为“摘录版本”的新 ACF 字段,默认情况下该字段为空。然后,我 运行 我的循环并说“如果摘录版本不是 v2
,请更新摘录并将版本设置为 v2
”。通过这样做,我可以保证逻辑只 运行 一次。您不必这样做,但如果您想 运行 分批处理代码(例如一次 10 个)以进行测试,这会很有帮助。如果您认为有一天您可能不得不再次更改摘录,它也会有所帮助。