wordpress修改附件表


function my_insert_attachment_data($data, $postarr){
	global $wpdb;
	$wpdb->posts = $wpdb->prefix . 'attachment';
	return $data;
}

function my_query($query){
	global $wpdb;

	$action = isset($_POST['action']) ? : '';

	if( strpos($_SERVER['HTTP_REFERER'], '/wp-admin/post.php') || strpos($_SERVER['HTTP_REFERER'], '/wp-admin/post-new.php') ){

		// 上传附件
		if( basename($_SERVER['REQUEST_URI']) == 'async-upload.php' ){
			if( $action == 'upload-attachment' ){
				add_filter('wp_insert_attachment_data', 'my_insert_attachment_data');
			}
		}

		// 编辑附件
		if( basename($_SERVER['REQUEST_URI']) == 'admin-ajax.php' ){
			if( in_array($action, array('delete-post', 'send-attachment-to-editor', 'save-attachment')) ){
				$wpdb->posts = $wpdb->prefix . 'attachment';
			}
		}

	}

	return $query;
}

add_filter('query', 'my_query');