您可以在所有帖子中运行一个循环,并更改它们的元值。为此,创建一个插件并在其激活时运行一个循环,使用get_posts()
:
<?php
/*
Plugin Name: Update MetaData for Posts
Description: Enable this plugin to update the metadata for all the posts
Author: JackJohansson
Version: 1.0
Author URI: http://example.com
*/
// Run the loop when the plugin is activated
register_activation_hook(__FILE__, \'update_my_metadata\');
function update_my_metadata(){
$args = array(
\'post_type\' => \'post\', // Only get the posts
\'post_status\' => \'publish\', // Only the posts that are published
\'posts_per_page\' => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
// Run a loop and update every meta data
update_post_meta( $post->ID, \'meta-key\', \'meta_value\' );
}
}
?>
将此代码保存到PHP文件中,并将其上载到插件目录。激活插件后,此代码将运行一次。然后您可以禁用或删除它。然而,安装这个插件并没有任何作用,因为它只在激活时运行。
你不喜欢插件吗?别担心
如果你不想把它作为插件使用,还有一种方法可以做到。您只需挂接到WordPress加载后运行的操作,然后运行您的函数:
function update_my_metadata(){
$args = array(
\'post_type\' => \'post\', // Only get the posts
\'post_status\' => \'publish\', // Only the posts that are published
\'posts_per_page\' => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
// Run a loop and update every meta data
update_post_meta( $post->ID, \'meta-key\', \'meta_value\' );
}
}
// Hook into init action and run our function
add_action(\'init\',\'update_my_metadata\');
但请注意,您必须在加载WordPress一次后删除代码。否则,它将在每个负载上反复运行。