更新附件图像标题和slug

时间:2015-08-28 10:41:17

标签: wordpress

我有更改附件图片标题(slug)但没有工作

function update_attachment($post_ID) {
$post=get_post($post_ID);
$old_name=$post->post_name;
$my_post = array(
'ID'           => $post_ID,
'name'   => $old_name."--".rand(1111,9999),
);
wp_update_post($my_post);

return $post_ID;
}
add_filter('add_attachment', 'update_attachment', 10, 2);

1 个答案:

答案 0 :(得分:1)

请试试这个

function update_attachment($post_ID) {
    $post=get_post($post_ID);
    $old_name=$post->post_name;
    global$wpdb;
    $wpdb->query("update ".$wpdb->prefix."posts set post_name='".$old_name."-".rand(1111,9999)."' where ID='".$post_ID."'");
    //var_dump($post_ID); exit;
    return $post_ID;
}

add_filter('add_attachment', 'update_attachment', 10, 2);