我正在尝试更改作者所有帖子的类别。我在这里看了大约一个小时,还没有找到解决办法。。
任何帮助都将不胜感激:)
谢谢,j03
我正在尝试更改作者所有帖子的类别。我在这里看了大约一个小时,还没有找到解决办法。。
任何帮助都将不胜感激:)
谢谢,j03
你可以用一个简单的脚本来实现这一点。重要!-在执行此操作之前备份数据库(&P);风险自负。
创建一个名为changeauthorcat的文件。php在您的主WordPress文件夹中。
<?php
include( \'wp-config.php\' );
global $wpdb;
// Author username
$username = \'exampleuser\';
// New category slug
$newCatSlug = \'examplecategory\';
// find the right author
$author = get_user_by( \'login\', $username );
// find the right category
$category = get_category_by_slug( $newCatSlug );
if ( $author && $category ) {
$posts = get_posts( array(
\'numberposts\' => -1,
\'author\' => $author->ID
)
);
if ( $posts ) {
foreach ( $posts as $post ) {
// update all categories
// the last parameter of false will replace all existing categories
// see https://codex.wordpress.org/Function_Reference/wp_set_post_categories
wp_set_post_categories( $post->ID, array( $category->term_id ), false );
}
}
}
?>
将“exampleuser”更改为要更改的作者的登录名。将“ExampleCography”更改为新类别的slug。导航到http://yourdomain.com/changeauthorcat.php.完成后删除文件。
请注意,这将从这些帖子中删除其他所有类别,并仅将它们放在新类别中。使用wp\\u set\\u post\\u categories功能也将使用正确的类别计数更新数据库。阅读更多信息:https://codex.wordpress.org/Function_Reference/wp_set_post_categories
您可以获取作者帖子id
ids =[]
ids = SELECT id FROM `wp_posts` WHERE `post_author`=1
然后可以循环结果new_category_id = 999
old_category_id = 111
for id in ids:
UPDATE `wp_term_relationships` SET `term_taxonomy_id`=new_id WHERE `object_id`=id AND `term_taxonomy_id`=old_category_id
or
wp_set_post_categories( $post_ID, $post_categories, $append )
我想获取所有自定义字段hidden 不存在。这不起作用:$postsForSitemap = get_posts(array( \'numberposts\' => -1, \'orderby\' => \'modified\', \'post_type\' => array(\'post\', \'page\'), \'order\' => \'DESC\', \'meta_key\' =&g