0

管理显示附加到特定类别的图像,独立于其父类别。现在希望将新上传的图像动态分配给一个类别。在 mySQL 中可以手动执行此操作:

INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES ('1000', '1', '0');

将以下内容添加到 functions.php 时,会产生上传错误:

    function image_category() {
        $wpdb->insert('wp_term_relationships', array(
        'object_id' => '1000',
        'term_taxonomy_id' => '26',
        'term_order' => '0'
    ));
}

显然,一旦成功,它将用新图像 ID 的变量替换 1000。

也试过:

$object_id = 1000;
$term_taxonomy_id = 26;
$term_order = 0;
   $wpdb->query( $wpdb->prepare( 
    "
        INSERT INTO $wpdb->wp_term_relationships
        ( object_id, term_taxonomy_id, term_order )
        VALUES ( %d, %s, %s )
    ", 
        array(
        10, 
        $object_id,
        $term_taxonomy_id, 
        $term_order
    ) 
) );
4

1 回答 1

0

有一个更容易的时间将其简化为:

add_action( 'add_attachment', 'image_category', 10 );
    function image_category($post_ID) {
        global $wpdb;

$term_taxonomy_id = 26;
$table = $wpdb->prefix.'term_relationships';
$data = array('object_id' => $post_ID, 'term_taxonomy_id' => $term_taxonomy_id, 'term_order' => 0);
$format = array('%d', '%s', '%s');
$wpdb->insert($table,$data,$format);
// $my_id = $wpdb->insert_id;
}
于 2020-03-12T02:08:07.280 回答