我正在使用Godaddy托管、PHP 5和Wordpress 4.2.1
我有34000多篇帖子需要删除过时的href标签。通过在页面模板中使用此代码并手动更改post\\u id,我可以删除标记,一次删除一篇文章。感谢@ialocin为wp kses添加内容。wp\\U Posteta表有347602行,但只有34788行有“simple\\u fields”行。
<?php $allowed_html = array(
\'p\' => array(),
\'img\' => array(
\'alt\' => true,
\'align\' => true,
\'border\' => true,
\'height\' => true,
\'hspace\' => true,
\'longdesc\' => true,
\'vspace\' => true,
\'src\' => true,
\'usemap\' => true,
\'width\' => true,
),
\'br\' => array()
);
?>
<?php $source = get_post_meta(24532, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', true); ?>
<?php update_post_meta(24532, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', wp_kses ($source, $allowed_html) ); ?>
但是,我需要将此应用于数千个wp\\U Posteta行。修改后this example 从@TheDeadMedic,我可以抛出以下代码的错误,但到目前为止,它对数据库没有任何作用。
错误是:
致命错误:允许的内存大小为67108864字节,在/wp包括/wp db。php在线2204
如果我将posts\\u per\\u页面更改为-1,它似乎没有任何作用。它只是刷新页面。
<?php
/**
* Update All postmeta
*
*/
$args = array(
\'fields\' => \'ids\', // MAGIC! Just get an array of id\'s, no objects committed to memory
\'posts_per_page\' => 500, // Batch size: look at all posts
\'post_type\' => \'post\',
\'date_query\' => array( // Make sure the post is over 30 days old
array(
\'column\' => \'post_date_gmt\',
\'before\' => \'1 month ago\'
)
),
);
$query = new WP_Query;
$paged = 1;
$count = 0;
$total = null;
do {
$args[\'no_found_rows\'] = isset( $total ); // No need to SQL_CALC_FOUND_ROWS on subsequent iterations
$args[\'paged\'] = $paged++;
$post_ids = $query->query( $args );
update_postmeta_cache( $post_ids ); // Get all meta for this group of posts
if ( ! isset( $total ) )
$total = $query->found_posts;
$count += $query->post_count;
foreach ( $post_ids as $post_id ) {
$allowed_html = array( // Declare tags not to be removed
\'p\' => array(),
\'img\' => array(
\'alt\' => true,
\'align\' => true,
\'border\' => true,
\'height\' => true,
\'hspace\' => true,
\'longdesc\' => true,
\'vspace\' => true,
\'src\' => true,
\'usemap\' => true,
\'width\' => true,
),
\'br\' => array()
);
$source = get_post_meta($post_id, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', true);
update_post_meta($$post_id, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', wp_kses ($source, $allowed_html) );
}
} while ( $count < $total );
?>
有人能看到有什么遗漏吗?“fields”=>“ids”,//神奇!从@TheDeadMedic需要是“post\\u id”,还是需要是foreach而不是while或other?
自动、动态、程序化或其他方式实现这一点的最佳方式是什么?
非常感谢您提供任何解决方案、线索、线索或提示。
最合适的回答,由SO网友:Lawrence1972 整理而成
最后,我能够遍历所有34788个条目,但只能根据发布日期以较小的批运行作业。这是我在页面模板上使用的最后一段代码。谢谢你@ialocin让我走上了正确的道路。这项工作已经完成,但必须有一种更好、更自动的方法来迭代帖子,而不必设置帖子日期或耗尽内存。如果你有更好的方法,请张贴在这里。
<?php
/**
* Remove href tags from postmeta by post date
*
*/
$args = array(
\'post_type\' => \'post\',
\'fields\' => \'ids\',
\'posts_per_page\' => -1, // Grab all Post IDs
\'date_query\' => array( // Set the date range of the posts you want effected
array(
\'after\' => \'June 1st, 2010\',
\'before\' => array(
\'year\' => 2012,
\'month\' => 12,
\'day\' => 30,
),
\'inclusive\' => true,
),
),
);
$query = new WP_Query($args);
if ($query->have_posts()):
foreach( $query->posts as $id ):
$allowed_html = array( // Declare tags not to be removed
\'p\' => array(),
\'img\' => array(
\'alt\' => true,
\'align\' => true,
\'border\' => true,
\'height\' => true,
\'hspace\' => true,
\'longdesc\' => true,
\'vspace\' => true,
\'src\' => true,
\'usemap\' => true,
\'width\' => true,
),
\'br\' => array()
);
// retrieve the postmeta for the specific custom field, only if it has data.
$source = get_post_meta($id,\'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', true);
// write it back, but remove the tags with wp_kses
update_post_meta($id, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', wp_kses ($source, $allowed_html) );
// Wipe this post\'s meta from memory
wp_cache_delete($id, \'post_meta\' );
endforeach;
endif;
?>